WebSockets and tokio working
This commit is contained in:
201
src/error.rs
201
src/error.rs
@ -1,120 +1,107 @@
|
||||
use std::fmt;
|
||||
use crate::usimp::{InputEnvelope, OutputEnvelope};
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub enum Kind {
|
||||
InvalidEndpointError,
|
||||
JsonParseError,
|
||||
DatabaseConnectionError,
|
||||
DatabaseError,
|
||||
HttpRequestParseError,
|
||||
IoError,
|
||||
WebSocketError,
|
||||
NotImplementedError,
|
||||
UsimpProtocolError,
|
||||
Utf8DecodeError,
|
||||
AuthenticationError,
|
||||
InvalidSessionError,
|
||||
use serde_json::{Value, Map};
|
||||
use bb8_postgres::tokio_postgres;
|
||||
use bb8_postgres;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Error {
|
||||
pub kind: ErrorKind,
|
||||
pub class: ErrorClass,
|
||||
pub msg: Option<String>,
|
||||
pub desc: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub enum Class {
|
||||
#[derive(Debug)]
|
||||
pub enum ErrorClass {
|
||||
ClientProtocolError,
|
||||
ClientError,
|
||||
ServerError,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Error {
|
||||
kind: Kind,
|
||||
msg: Option<String>,
|
||||
desc: Option<String>,
|
||||
class: Class,
|
||||
pub enum ErrorKind {
|
||||
NotImplemented,
|
||||
UsimpError,
|
||||
WebSocketError,
|
||||
DatabaseError,
|
||||
}
|
||||
|
||||
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: Kind, class: Class) -> Self {
|
||||
Error {
|
||||
pub fn new(kind: ErrorKind, class: ErrorClass, msg: Option<String>) -> Self {
|
||||
return Error {
|
||||
kind,
|
||||
msg: None,
|
||||
desc: None,
|
||||
class,
|
||||
msg,
|
||||
desc: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn class(&self) -> &Class {
|
||||
&self.class
|
||||
}
|
||||
|
||||
pub fn set_msg(mut self, msg: String) -> Self {
|
||||
pub fn msg(&mut self, msg: String) {
|
||||
self.msg = Some(msg);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn msg(&self) -> &str {
|
||||
match &self.msg {
|
||||
Some(msg) => msg.as_str(),
|
||||
None => match self.kind {
|
||||
Kind::InvalidEndpointError => "Invalid endpoint",
|
||||
Kind::JsonParseError => "Unable to parse JSON data",
|
||||
Kind::DatabaseConnectionError => "Unable to connect to database",
|
||||
Kind::DatabaseError => "Database error",
|
||||
Kind::HttpRequestParseError => "Unable to parse http request",
|
||||
Kind::IoError => "IO error",
|
||||
Kind::WebSocketError => "WebSocket protocol error",
|
||||
Kind::NotImplementedError => "Not yet implemented",
|
||||
Kind::UsimpProtocolError => "USIMP protocol error",
|
||||
Kind::Utf8DecodeError => "Unable to decode UTF-8 data",
|
||||
Kind::AuthenticationError => "Unable to authenticate",
|
||||
Kind::InvalidSessionError => "Invalid session",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_desc(mut self, desc: String) -> Self {
|
||||
self.desc = Some(desc);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn desc(&self) -> Option<&str> {
|
||||
match &self.desc {
|
||||
Some(desc) => Some(desc.as_str()),
|
||||
None => None,
|
||||
pub fn code(&self) -> &str {
|
||||
match self.kind {
|
||||
ErrorKind::NotImplemented => "NOT_IMPLEMENTED",
|
||||
ErrorKind::UsimpError => "USIMP_ERROR",
|
||||
ErrorKind::WebSocketError => "WEBSOCKET_ERROR",
|
||||
ErrorKind::DatabaseError => "BACKEND_ERROR",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let mut error = match self.kind {
|
||||
Kind::InvalidEndpointError => "invalid endpoint",
|
||||
Kind::JsonParseError => "unable to parse json data",
|
||||
Kind::DatabaseConnectionError => "unable to connect to database",
|
||||
Kind::DatabaseError => "database error",
|
||||
Kind::HttpRequestParseError => "unable to parse http request",
|
||||
Kind::IoError => "io error",
|
||||
Kind::WebSocketError => "websocket protocol error",
|
||||
Kind::NotImplementedError => "not yet implemented",
|
||||
Kind::UsimpProtocolError => "usimp protocol error",
|
||||
Kind::Utf8DecodeError => "unable to decode utf-8 data",
|
||||
Kind::AuthenticationError => "unable to authenticate",
|
||||
Kind::InvalidSessionError => "invalid session",
|
||||
impl From<Error> for OutputEnvelope {
|
||||
fn from(error: Error) -> Self {
|
||||
OutputEnvelope {
|
||||
error: Some(error),
|
||||
data: Value::Null,
|
||||
request_nr: None,
|
||||
}
|
||||
.to_string();
|
||||
if let Some(desc) = &self.desc {
|
||||
error += ": ";
|
||||
error += desc;
|
||||
}
|
||||
write!(f, "{}", error)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for Error {
|
||||
fn from(error: std::io::Error) -> Self {
|
||||
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: Kind::IoError,
|
||||
msg: Some(error.to_string()),
|
||||
kind: ErrorKind::UsimpError,
|
||||
class: ErrorClass::ClientProtocolError,
|
||||
msg: None,
|
||||
desc: Some(error.to_string()),
|
||||
class: Class::ClientProtocolError,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -122,45 +109,43 @@ impl From<std::io::Error> for Error {
|
||||
impl From<serde_json::Error> for Error {
|
||||
fn from(error: serde_json::Error) -> Self {
|
||||
Error {
|
||||
kind: Kind::JsonParseError,
|
||||
msg: Some("Unable to parse JSON data".to_string()),
|
||||
kind: ErrorKind::UsimpError,
|
||||
class: ErrorClass::ClientProtocolError,
|
||||
msg: None,
|
||||
desc: Some(error.to_string()),
|
||||
class: Class::ClientProtocolError,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<r2d2::Error> for Error {
|
||||
fn from(error: r2d2::Error) -> Self {
|
||||
impl From<hyper::Error> for Error {
|
||||
fn from(error: hyper::Error) -> Self {
|
||||
Error {
|
||||
kind: Kind::DatabaseConnectionError,
|
||||
msg: Some("Unable to connect to database".to_string()),
|
||||
kind: ErrorKind::UsimpError,
|
||||
class: ErrorClass::ClientProtocolError,
|
||||
msg: None,
|
||||
desc: Some(error.to_string()),
|
||||
class: Class::ServerError,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<r2d2_postgres::postgres::Error> for Error {
|
||||
fn from(error: r2d2_postgres::postgres::Error) -> Self {
|
||||
// format: "db error: ERROR ..."
|
||||
let msg = error.to_string().split(":").skip(1).collect::<String>();
|
||||
impl From<tokio_postgres::Error> for Error {
|
||||
fn from(error: tokio_postgres::Error) -> Self {
|
||||
Error {
|
||||
kind: Kind::DatabaseError,
|
||||
msg: Some("Database error".to_string()),
|
||||
desc: Some(msg.trim().to_string()),
|
||||
class: Class::ServerError,
|
||||
kind: ErrorKind::DatabaseError,
|
||||
class: ErrorClass::ServerError,
|
||||
msg: None,
|
||||
desc: Some(error.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::string::FromUtf8Error> for Error {
|
||||
fn from(error: std::string::FromUtf8Error) -> Self {
|
||||
impl From<bb8_postgres::bb8::RunError<tokio_postgres::Error>> for Error {
|
||||
fn from(error: bb8_postgres::bb8::RunError<tokio_postgres::Error>) -> Self {
|
||||
Error {
|
||||
kind: Kind::Utf8DecodeError,
|
||||
msg: Some("Unable to decode UTF-8 data".to_string()),
|
||||
kind: ErrorKind::DatabaseError,
|
||||
class: ErrorClass::ServerError,
|
||||
msg: None,
|
||||
desc: Some(error.to_string()),
|
||||
class: Class::ClientProtocolError,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user