186 lines
4.6 KiB
Rust
186 lines
4.6 KiB
Rust
use crate::usimp::{Event, InputEnvelope, OutputEnvelope};
|
|
|
|
use bb8_postgres;
|
|
use bb8_postgres::tokio_postgres;
|
|
use serde_json::{Map, Value};
|
|
|
|
#[derive(Debug)]
|
|
pub struct Error {
|
|
pub kind: ErrorKind,
|
|
pub class: ErrorClass,
|
|
pub msg: Option<String>,
|
|
pub desc: Option<String>,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum ErrorClass {
|
|
ClientProtocolError,
|
|
ClientError,
|
|
ServerError,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum ErrorKind {
|
|
NotImplemented,
|
|
UsimpError,
|
|
WebSocketError,
|
|
DatabaseError,
|
|
InvalidSessionError,
|
|
AuthenticationError,
|
|
SubscriptionError,
|
|
InternalError,
|
|
}
|
|
|
|
impl InputEnvelope {
|
|
pub fn new_error(
|
|
&self,
|
|
kind: ErrorKind,
|
|
class: ErrorClass,
|
|
msg: Option<String>,
|
|
) -> OutputEnvelope {
|
|
OutputEnvelope {
|
|
request_nr: self.request_nr,
|
|
error: Some(Error::new(kind, class, msg)),
|
|
data: Value::Null,
|
|
}
|
|
}
|
|
|
|
pub fn error(&self, error: Error) -> OutputEnvelope {
|
|
OutputEnvelope {
|
|
request_nr: self.request_nr,
|
|
error: Some(error),
|
|
data: Value::Null,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Error {
|
|
pub fn new(kind: ErrorKind, class: ErrorClass, msg: Option<String>) -> Self {
|
|
return Error {
|
|
kind,
|
|
class,
|
|
msg,
|
|
desc: None,
|
|
};
|
|
}
|
|
|
|
pub fn msg(&mut self, msg: String) {
|
|
self.msg = Some(msg);
|
|
}
|
|
|
|
pub fn code(&self) -> &str {
|
|
match self.kind {
|
|
ErrorKind::NotImplemented => "NOT_IMPLEMENTED",
|
|
ErrorKind::UsimpError => "USIMP_ERROR",
|
|
ErrorKind::WebSocketError => "WEBSOCKET_ERROR",
|
|
ErrorKind::DatabaseError | ErrorKind::InternalError => "SERVER_ERROR",
|
|
ErrorKind::InvalidSessionError => "INVALID_SESSION_ERROR",
|
|
ErrorKind::AuthenticationError => "AUTHENTICATION_ERROR",
|
|
ErrorKind::SubscriptionError => "SUBSCRIPTION_ERROR",
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<Error> for OutputEnvelope {
|
|
fn from(error: Error) -> Self {
|
|
OutputEnvelope {
|
|
error: Some(error),
|
|
data: Value::Null,
|
|
request_nr: None,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<Error> for Value {
|
|
fn from(error: Error) -> Self {
|
|
let mut obj = Value::Object(Map::new());
|
|
obj["code"] = Value::from(error.code());
|
|
obj["message"] = match error.msg {
|
|
Some(msg) => Value::from(msg),
|
|
None => Value::Null,
|
|
};
|
|
obj["description"] = match error.desc {
|
|
Some(desc) => Value::from(desc),
|
|
None => Value::Null,
|
|
};
|
|
obj
|
|
}
|
|
}
|
|
|
|
impl From<hyper::header::ToStrError> for Error {
|
|
fn from(error: hyper::header::ToStrError) -> Self {
|
|
Error {
|
|
kind: ErrorKind::UsimpError,
|
|
class: ErrorClass::ClientProtocolError,
|
|
msg: None,
|
|
desc: Some(error.to_string()),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<serde_json::Error> for Error {
|
|
fn from(error: serde_json::Error) -> Self {
|
|
Error {
|
|
kind: ErrorKind::UsimpError,
|
|
class: ErrorClass::ClientProtocolError,
|
|
msg: None,
|
|
desc: Some(error.to_string()),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<hyper::Error> for Error {
|
|
fn from(error: hyper::Error) -> Self {
|
|
Error {
|
|
kind: ErrorKind::UsimpError,
|
|
class: ErrorClass::ClientProtocolError,
|
|
msg: None,
|
|
desc: Some(error.to_string()),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<tokio_postgres::Error> for Error {
|
|
fn from(error: tokio_postgres::Error) -> Self {
|
|
Error {
|
|
kind: ErrorKind::DatabaseError,
|
|
class: ErrorClass::ServerError,
|
|
msg: None,
|
|
desc: Some(error.to_string()),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<bb8_postgres::bb8::RunError<tokio_postgres::Error>> for Error {
|
|
fn from(error: bb8_postgres::bb8::RunError<tokio_postgres::Error>) -> Self {
|
|
Error {
|
|
kind: ErrorKind::DatabaseError,
|
|
class: ErrorClass::ServerError,
|
|
msg: None,
|
|
desc: Some(error.to_string()),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<tokio::sync::mpsc::error::SendError<Event>> for Error {
|
|
fn from(error: tokio::sync::mpsc::error::SendError<Event>) -> Self {
|
|
Error {
|
|
kind: ErrorKind::InternalError,
|
|
class: ErrorClass::ServerError,
|
|
msg: None,
|
|
desc: Some(error.to_string()),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<uuid::Error> for Error {
|
|
fn from(error: uuid::Error) -> Self {
|
|
Error {
|
|
kind: ErrorKind::UsimpError,
|
|
class: ErrorClass::ClientError,
|
|
msg: None,
|
|
desc: Some(error.to_string()),
|
|
}
|
|
}
|
|
}
|