Split errors into own lib file

This commit is contained in:
Gabriel Simmer 2023-11-17 21:33:40 +00:00
parent a82b21b2ce
commit d8e00da078
Signed by: arch
SSH key fingerprint: SHA256:m3OEcdtrnBpMX+2BDGh/byv3hrCekCLzDYMdvGEKPPQ
2 changed files with 57 additions and 55 deletions

55
src/lib.rs Normal file
View file

@ -0,0 +1,55 @@
use std::{io, env, fmt, error::Error};
use toml::{self, de};
#[derive(Debug)]
pub enum WsError {
Reqwest(reqwest::Error),
Url(url::ParseError),
Io(io::Error),
Toml(de::Error),
Var(env::VarError),
}
impl From<url::ParseError> for WsError {
fn from(value: url::ParseError) -> Self {
Self::Url(value)
}
}
impl From<reqwest::Error> for WsError {
fn from(value: reqwest::Error) -> Self {
Self::Reqwest(value)
}
}
impl From<io::Error> for WsError {
fn from(value: io::Error) -> Self {
Self::Io(value)
}
}
impl From<toml::de::Error> for WsError {
fn from(value: toml::de::Error) -> Self {
Self::Toml(value)
}
}
impl From<env::VarError> for WsError {
fn from(value: env::VarError) -> Self {
Self::Var(value)
}
}
impl Error for WsError {}
impl fmt::Display for WsError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
WsError::Reqwest(e) => write!(f, "reqwest error: {}", e),
WsError::Url(e) => write!(f, "url error: {}", e),
WsError::Io(e) => write!(f, "io error: {}", e),
WsError::Toml(e) => write!(f, "toml error: {}", e),
WsError::Var(e) => write!(f, "var error: {}", e),
}
}
}

View file

@ -14,10 +14,10 @@ use prometheus::{
}; };
use reqwest::header::USER_AGENT; use reqwest::header::USER_AGENT;
use serde::Deserialize; use serde::Deserialize;
use vrchat_prometheus_adapter::WsError;
use std::collections::HashMap; use std::collections::HashMap;
use std::error::Error;
use std::sync::OnceLock; use std::sync::OnceLock;
use std::{env, fmt, fs, io}; use std::{env, fs};
use url::Url; use url::Url;
static PLAYER_COUNT: OnceLock<GaugeVec> = OnceLock::new(); static PLAYER_COUNT: OnceLock<GaugeVec> = OnceLock::new();
@ -26,59 +26,6 @@ static WORLD_VISITS: OnceLock<IntCounterVec> = OnceLock::new();
static WORLD_OCCUPANTS: OnceLock<GaugeVec> = OnceLock::new(); static WORLD_OCCUPANTS: OnceLock<GaugeVec> = OnceLock::new();
static WORLD_FAVORITES: OnceLock<IntCounterVec> = OnceLock::new(); static WORLD_FAVORITES: OnceLock<IntCounterVec> = OnceLock::new();
#[derive(Debug)]
enum WsError {
Reqwest(reqwest::Error),
Url(url::ParseError),
Io(io::Error),
Toml(toml::de::Error),
Var(env::VarError),
}
impl From<url::ParseError> for WsError {
fn from(value: url::ParseError) -> Self {
Self::Url(value)
}
}
impl From<reqwest::Error> for WsError {
fn from(value: reqwest::Error) -> Self {
Self::Reqwest(value)
}
}
impl From<io::Error> for WsError {
fn from(value: io::Error) -> Self {
Self::Io(value)
}
}
impl From<toml::de::Error> for WsError {
fn from(value: toml::de::Error) -> Self {
Self::Toml(value)
}
}
impl From<env::VarError> for WsError {
fn from(value: env::VarError) -> Self {
Self::Var(value)
}
}
impl Error for WsError {}
impl fmt::Display for WsError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
WsError::Reqwest(e) => write!(f, "reqwest error: {}", e),
WsError::Url(e) => write!(f, "url error: {}", e),
WsError::Io(e) => write!(f, "io error: {}", e),
WsError::Toml(e) => write!(f, "toml error: {}", e),
WsError::Var(e) => write!(f, "var error: {}", e),
}
}
}
#[derive(Clone, Debug, Deserialize)] #[derive(Clone, Debug, Deserialize)]
struct Config { struct Config {
/// Groups we want to track. /// Groups we want to track.