server/src/error.rs

52 lines
1.0 KiB
Rust

use std::fmt::Display;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum ErrorKind {
Internal,
}
#[derive(Debug)]
pub struct Error {
kind: ErrorKind,
code: u16,
message: String,
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.message)
}
}
impl Error {
pub fn internal(code: u16, s: &str) -> Self {
Self {
kind: ErrorKind::Internal,
code,
message: s.to_string(),
}
}
}
impl From<indradb::Error> for Error {
fn from(source: indradb::Error) -> Self {
Self {
kind: ErrorKind::Internal,
code: 0,
message: format!("{}", source),
}
}
}
impl From<indradb::ValidationError> for Error {
fn from(source: indradb::ValidationError) -> Self {
Self {
kind: ErrorKind::Internal,
code: 1,
message: format!("{}", source),
}
}
}