all MizeError's are now constructed with the new() function instead of with directly with {}
This commit is contained in:
70
src/error.rs
70
src/error.rs
@@ -1,24 +1,63 @@
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::string::FromUtf8Error;
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
static ref ERRORS: HashMap<u32, MizeError> = parse_errors();
|
||||
}
|
||||
|
||||
fn parse_errors() -> HashMap<u32, MizeError>{
|
||||
let toml_string = include_str!("../errors.toml");
|
||||
}
|
||||
|
||||
use json;
|
||||
use toml;
|
||||
use crate::server::proto;
|
||||
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MizeError {
|
||||
pub kind: String,
|
||||
pub category: String,
|
||||
pub code: u32,
|
||||
pub message: String,
|
||||
pub extra_msg: Option<String>,
|
||||
}
|
||||
|
||||
impl MizeError {
|
||||
pub fn new(code: u32) -> MizeError {
|
||||
//let error = ERRORS.
|
||||
|
||||
MizeError {
|
||||
category: "error with error system".to_string(),
|
||||
code: 114,
|
||||
message: "The error with code {} was not found in the errors.toml file".to_string(),
|
||||
extra_msg: None
|
||||
}
|
||||
}
|
||||
|
||||
// pub fn format(&self, values: [str]){
|
||||
// }
|
||||
|
||||
pub fn extra_msg(mut self, msg: &str) -> MizeError{
|
||||
self.extra_msg = Some(msg.to_string());
|
||||
return self;
|
||||
}
|
||||
|
||||
pub fn to_json(self: MizeError) -> json::JsonValue{
|
||||
let data = json::object!{
|
||||
kind: self.kind,
|
||||
code: self.code,
|
||||
message: self.message,
|
||||
};
|
||||
return data;
|
||||
if let Some(extra_msg) = self.extra_msg {
|
||||
json::object!{
|
||||
category: self.category,
|
||||
code: self.code,
|
||||
message: self.message,
|
||||
extra_msg: extra_msg,
|
||||
}
|
||||
} else {
|
||||
json::object!{
|
||||
category: self.category,
|
||||
code: self.code,
|
||||
message: self.message,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_message(self: MizeError, origin: proto::Origin) -> proto::Message {
|
||||
@@ -27,28 +66,17 @@ impl MizeError {
|
||||
msg.extend(json::stringify(data).into_bytes());
|
||||
return proto::Message::from_bytes(msg, origin);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl From<FromUtf8Error> for MizeError {
|
||||
fn from(utf_err: FromUtf8Error) -> MizeError{
|
||||
let err = MizeError {
|
||||
code: 109,
|
||||
kind: "faulty_message".to_string(),
|
||||
message: "std::string::FromUtf8Error while parsing something from the message".to_string(),
|
||||
};
|
||||
return err;
|
||||
MizeError::new(109).extra_msg("std::string::FromUtf8Error while parsing something from the message")
|
||||
}
|
||||
}
|
||||
|
||||
impl From<surrealdb::Error> for MizeError {
|
||||
fn from(sur_err: surrealdb::Error) -> MizeError{
|
||||
let cat = format!("data-storage:surrealdb:{:?}", sur_err);
|
||||
let msg = format!("Something went wrong internally with the data-storage:surrealdb: {}", sur_err);
|
||||
let err = MizeError {
|
||||
code: 128,
|
||||
kind: cat,
|
||||
message: msg,
|
||||
};
|
||||
return err;
|
||||
MizeError::new(129)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ use futures_util::{FutureExt, StreamExt, SinkExt};
|
||||
use std::collections::HashMap;
|
||||
use std::boxed::Box;
|
||||
use std::path::Path;
|
||||
use crate::error;
|
||||
use crate::error::MizeError;
|
||||
|
||||
use tokio::sync::mpsc::{Sender, channel, Receiver};
|
||||
@@ -133,6 +134,10 @@ impl Mutexes {
|
||||
}
|
||||
}
|
||||
|
||||
fn test(){
|
||||
println!("test func");
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
pub async fn run_server(args: Vec<String>) {
|
||||
/*
|
||||
@@ -152,6 +157,13 @@ pub async fn run_server(args: Vec<String>) {
|
||||
}
|
||||
}
|
||||
|
||||
//load the errors.toml file and init the error system
|
||||
//let error_toml_file = include_str!("../errors.toml");
|
||||
//println!("ERRORS: {:?}", crate::error::ERRORS);
|
||||
error::init();
|
||||
println!("hello in run_server");
|
||||
test();
|
||||
|
||||
//create the itemstore
|
||||
let itemstore = crate::server::itemstore::Itemstore::new(mize_folder.clone() + "/db").await.expect("error creating itemstore");
|
||||
|
||||
@@ -335,11 +347,9 @@ async fn handle_websocket_connection(
|
||||
if let Ok(name) = mod_header_val.to_str() {
|
||||
module.name = name.to_string();
|
||||
} else {
|
||||
let err = MizeError{
|
||||
code: 113,
|
||||
kind: "don't know yet".to_string(),
|
||||
message: "The mize-module Header could not be decoded into a String.".to_string(),
|
||||
};
|
||||
let err = MizeError::new(113)
|
||||
.extra_msg("The mize-module Header could not be decoded into a String.");
|
||||
|
||||
msg_tx.send(err.to_message(proto::Origin::Module(module.clone()))).await;
|
||||
return;
|
||||
};
|
||||
@@ -389,11 +399,9 @@ async fn handle_websocket_connection(
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
let err = MizeError{
|
||||
code: 11,
|
||||
kind: "don't know yet".to_string(),
|
||||
message: "the message type was not Binary".to_string(),
|
||||
};
|
||||
let err = MizeError::new(11)
|
||||
.extra_msg("the message type was not Binary");
|
||||
|
||||
msg_tx.send(err.to_message(origin.clone())).await;
|
||||
vec![0]
|
||||
},
|
||||
|
||||
@@ -94,11 +94,8 @@ impl Itemstore {
|
||||
let mut old_keys = match tr.get(format!("{}", update.id).into_bytes()).await? {
|
||||
Some(keys) => decode(keys),
|
||||
None => {
|
||||
return Err(MizeError{
|
||||
code: 104,
|
||||
kind: "data_storage::index_not_found".to_string(),
|
||||
message: "Internal Datastorage Error: the Index of the Item was not found".to_string(),
|
||||
});
|
||||
return Err(MizeError::new(104)
|
||||
.extra_msg("Internal Datastorage Error: the Index of the Item was not found"));
|
||||
},
|
||||
};
|
||||
|
||||
@@ -146,12 +143,8 @@ impl Itemstore {
|
||||
let id_u64: u64 = match id_str.parse() {
|
||||
Ok(id) => id,
|
||||
Err(err) => {
|
||||
return Err(MizeError {
|
||||
code: 110,
|
||||
kind: "faulty_message".to_string(),
|
||||
message: format!("std::num::ParseIntError while parsing id \"{}\" into an Integer", id_str),
|
||||
|
||||
});
|
||||
return Err(MizeError::new(110)
|
||||
.extra_msg("std::num::ParseIntError while parsing id \"{}\" into an Integer"));
|
||||
}
|
||||
};
|
||||
let mut tr = self.db.transaction(true, true).await?;
|
||||
@@ -162,11 +155,9 @@ impl Itemstore {
|
||||
tr.del(key).await?;
|
||||
}
|
||||
} else {
|
||||
return Err(MizeError{
|
||||
code: 104,
|
||||
kind: "data_storage::index_not_found".to_string(),
|
||||
message: "Internal Datastorage Error: the Index of the Item was not found, This means most likely that this item does not exist.".to_string(),
|
||||
});
|
||||
return Err(MizeError::new(104)
|
||||
.extra_msg("Internal Datastorage Error: the Index of the Item was not found, \
|
||||
This means most likely that this item does not exist."));
|
||||
}
|
||||
|
||||
tr.del(id_str.into_bytes()).await?;
|
||||
@@ -197,11 +188,8 @@ impl Itemstore {
|
||||
};
|
||||
return Ok(item);
|
||||
} else {
|
||||
return Err(MizeError{
|
||||
code: 104,
|
||||
kind: "data_storage::index_not_found".to_string(),
|
||||
message: "Internal Datastorage Error: the Index of the Item was not found".to_string(),
|
||||
});
|
||||
return Err(MizeError::new(104)
|
||||
.extra_msg("Internal Datastorage Error: the Index of the Item was not found"));
|
||||
}
|
||||
return Ok(item);
|
||||
}
|
||||
|
||||
@@ -126,11 +126,7 @@ pub enum DeltaCmd {
|
||||
|
||||
impl Delta {
|
||||
pub fn from_bytes(bytes: Vec<u8>) -> Result<Delta, MizeError> {
|
||||
let out_of_bytes = Err(MizeError{
|
||||
code: 107,
|
||||
kind: "faulty_message".to_string(),
|
||||
message: "There were not eneough Bytes in the message to complete a Delta.".to_string()
|
||||
});
|
||||
let out_of_bytes = Err(MizeError::new(107).extra_msg("There were not eneough Bytes in the message to complete a Delta."));
|
||||
let mut bytes = bytes.iter();
|
||||
let mut delta = Delta{raw: Vec::new()};
|
||||
|
||||
@@ -183,11 +179,9 @@ impl Delta {
|
||||
delta.raw.push(DeltaCmd::Delete(start, stop));
|
||||
},
|
||||
_ => {
|
||||
return Err(MizeError{
|
||||
code: 107,
|
||||
kind: "faulty_message".to_string(),
|
||||
message: format!("The update message included a DeltaCmd of {}, only 0,1,2 (for: replace, insert, delete) are allowed.",cmd)
|
||||
});
|
||||
return Err(MizeError::new(107)
|
||||
.extra_msg(&format!("The update message included a DeltaCmd of {}.\
|
||||
Only 0,1,2 (for: replace, insert, delete) are allowed.", cmd)));
|
||||
},
|
||||
}
|
||||
};
|
||||
@@ -203,11 +197,9 @@ impl Update {
|
||||
let id_u64 = match id.parse::<u64>(){
|
||||
Ok(id) => id,
|
||||
Err(err) => {
|
||||
return Err(MizeError{
|
||||
code: 11,
|
||||
kind: "don't know yet".to_string(),
|
||||
message: "Error while parsing id to u64 (std::num::ParseIntError) in order to get it from the itemstore. (get or get_sub message)".to_string(),
|
||||
});
|
||||
return Err(MizeError::new(11)
|
||||
.extra_msg("Error while parsing id to u64 (std::num::ParseIntError) in order to get it from the itemstore.\
|
||||
(get or get_sub message)"));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -393,18 +385,10 @@ impl Message {
|
||||
|
||||
pub fn get_metadata(&mut self) -> Result<HashMap<String, String>, MizeError> {
|
||||
if !self.has_meta {
|
||||
return Err(MizeError {
|
||||
code: 108,
|
||||
kind: "faulty_message".to_string(),
|
||||
message: "The message should have medatata, but it hasn't. (cmd <= 128)".to_string(),
|
||||
})
|
||||
return Err(MizeError::new(108).extra_msg("The message should have medatata, but it hasn't. (cmd <= 128)"))
|
||||
};
|
||||
|
||||
let meta_err = MizeError {
|
||||
code: 107,
|
||||
kind: "faulty_message".to_string(),
|
||||
message: "Error while parsing the metadata of a message.".to_string(),
|
||||
};
|
||||
let meta_err = MizeError::new(107).extra_msg("Error while parsing the metadata of a message.");
|
||||
|
||||
let mut meta_vec:Vec<u8> = Vec::new();
|
||||
let mut msg_iter = self.raw[self.index..].iter();
|
||||
@@ -433,11 +417,7 @@ impl Message {
|
||||
meta_vec.push(*ch);
|
||||
};
|
||||
|
||||
return Err(MizeError {
|
||||
code: 107,
|
||||
kind: "faulty_message".to_string(),
|
||||
message: "Error while parsing the metadata of a message.".to_string(),
|
||||
})
|
||||
return Err(MizeError::new(107).extra_msg("Error while parsing the metadata of a message."))
|
||||
|
||||
}
|
||||
|
||||
@@ -463,11 +443,7 @@ impl Message {
|
||||
}
|
||||
}
|
||||
|
||||
return Err(MizeError {
|
||||
code: 112,
|
||||
kind: "faulty_message".to_string(),
|
||||
message: "There is no / to indicate where the id ends".to_string(),
|
||||
});
|
||||
return Err(MizeError::new(112).extra_msg("There is no / to indicate where the id ends"));
|
||||
|
||||
// let len = self.raw.len();
|
||||
// while self.index < 10000 {
|
||||
@@ -548,11 +524,8 @@ pub async fn handle_mize_message(
|
||||
|
||||
//forward to module in case the item is from a Module
|
||||
//append medatata
|
||||
let first_char = id.chars().nth(0).ok_or(MizeError{
|
||||
code: 11,
|
||||
kind: "don't know yet".to_string(),
|
||||
message: "Somehow the id is empty. while handling a get or get_sub message.".to_string(),
|
||||
})?;
|
||||
let first_char = id.chars().nth(0)
|
||||
.ok_or(MizeError::new(11).extra_msg("Somehow the id is empty. while handling a get or get_sub message."))?;
|
||||
|
||||
if (first_char == '!'){
|
||||
message.clone().forward_module(id.clone(), mutexes.clone()).await;
|
||||
@@ -583,11 +556,9 @@ pub async fn handle_mize_message(
|
||||
let id_u64 = match id.parse::<u64>(){
|
||||
Ok(id) => id,
|
||||
Err(err) => {
|
||||
return Err(MizeError{
|
||||
code: 11,
|
||||
kind: "don't know yet".to_string(),
|
||||
message: "Error while parsing id to u64 in order to get it from the itemstore. (get or get_sub message)".to_string(),
|
||||
});
|
||||
return Err(MizeError::new(11)
|
||||
.extra_msg("Error while parsing id to u64 in order to get it from the itemstore.\
|
||||
(get or get_sub message)"));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -620,18 +591,14 @@ pub async fn handle_mize_message(
|
||||
if (message.cmd == MSG_GIVE && matches!(&message.origin, Origin::Module(_))){
|
||||
let meta = message.get_metadata()?;
|
||||
let id: String = message.get_id()?;
|
||||
let client_id_str = meta.get("c").ok_or(MizeError{
|
||||
code: 11,
|
||||
kind: "don't know yet".to_string(),
|
||||
message: "there is no \"c\" key in the metadata of a message gotten from a module".to_string(),
|
||||
})?;
|
||||
|
||||
let client_id: u64 = client_id_str.parse().map_err(|_| MizeError{
|
||||
code: 11,
|
||||
kind: "don't know yet".to_string(),
|
||||
message: "Error (std::num::ParseIntError) while parsing the \"c\" key in the Message metadata from a give Message from a Module".to_string()
|
||||
})?;
|
||||
let client_id_str = meta.get("c")
|
||||
.ok_or(MizeError::new(11)
|
||||
.extra_msg("there is no \"c\" key in the metadata of a message gotten from a module"))?;
|
||||
|
||||
let client_id: u64 = client_id_str.parse()
|
||||
.map_err(|_| MizeError::new(11)
|
||||
.extra_msg("Error (std::num::ParseIntError) while parsing the \"c\" key \
|
||||
in the Message metadata from a give Message from a Module"))?;
|
||||
|
||||
let clients = mutexes.clients.lock().await;
|
||||
let modules = mutexes.modules.lock().await;
|
||||
@@ -646,11 +613,7 @@ pub async fn handle_mize_message(
|
||||
} else if (client.len() == 1) {
|
||||
Origin::Client(client[0].to_owned())
|
||||
} else {
|
||||
return Err(MizeError{
|
||||
code: 11,
|
||||
kind: "don't know yet".to_string(),
|
||||
message: "".to_string()
|
||||
})
|
||||
return Err(MizeError::new(11))
|
||||
};
|
||||
|
||||
drop(clients);
|
||||
@@ -694,11 +657,9 @@ pub async fn handle_mize_message(
|
||||
if (message.cmd == MSG_UPDATE_REQUEST && (matches!(&message.origin, Origin::Module(_)) || matches!(&message.origin, Origin::Client(_)))){
|
||||
|
||||
let id: String = message.get_id()?;
|
||||
let first_char = id.chars().nth(0).ok_or(MizeError{
|
||||
code: 11,
|
||||
kind: "don't know yet".to_string(),
|
||||
message: "Somehow the id is empty. while handling a get or get_sub message.".to_string(),
|
||||
})?;
|
||||
let first_char = id.chars().nth(0)
|
||||
.ok_or(MizeError::new(11)
|
||||
.extra_msg("Somehow the id is empty. while handling a get or get_sub message."))?;
|
||||
|
||||
if (first_char == '!'){
|
||||
message.clone().forward_module(id, mutexes.clone()).await;
|
||||
@@ -715,11 +676,7 @@ pub async fn handle_mize_message(
|
||||
if (message.cmd == MSG_DELETE && (matches!(&message.origin, Origin::Module(_)) || matches!(&message.origin, Origin::Client(_)))){
|
||||
let id: String = message.get_id()?;
|
||||
if id == "0".to_string() {
|
||||
let err = error::MizeError {
|
||||
kind: "don't know yet".to_string(),
|
||||
code: 100,
|
||||
message: "You can't delete item number 0. This item contains mandatory Config for the Server.".to_string(),
|
||||
};
|
||||
let err = MizeError::new(100);
|
||||
return Err(err);
|
||||
}
|
||||
return Ok(());
|
||||
@@ -814,24 +771,20 @@ pub async fn handle_update(mut update: Update, mutexes: Mutexes, origin: Origin)
|
||||
let id_u64 = match update.id.clone().parse::<u64>(){
|
||||
Ok(id) => id,
|
||||
Err(err) => {
|
||||
return Err(MizeError{
|
||||
code: 11,
|
||||
kind: "don't know yet".to_string(),
|
||||
message: "Error while parsing id to u64 in order to get it from the itemstore. (get or get_sub message)".to_string(),
|
||||
});
|
||||
return Err(MizeError::new(11)
|
||||
.extra_msg("Error while parsing id to u64 in order to get it from the itemstore.\
|
||||
(get or get_sub message)"));
|
||||
}
|
||||
};
|
||||
let item = itemstore.get(id_u64).await?;
|
||||
let commit_pos = item.iter().position(|field| field[0] == "_commit".to_string().into_bytes()).ok_or(MizeError{
|
||||
code: 103,
|
||||
kind: "key_missing::_commit".to_string(),
|
||||
message: format!("Item {} has not _commit", id_u64),
|
||||
})?;
|
||||
let cur_commit = u64::from_be_bytes(item[commit_pos][1].clone().try_into().map_err(|_|MizeError{
|
||||
code: 105,
|
||||
kind: "don't know yet".to_string(),
|
||||
message: "_commit is not 8 bytes long".to_string(),
|
||||
})?);
|
||||
let commit_pos = item.iter()
|
||||
.position(|field| field[0] == "_commit".to_string().into_bytes())
|
||||
.ok_or(MizeError::new(103)
|
||||
.extra_msg(&format!("Item {} has not _commit", id_u64)))?;
|
||||
|
||||
let cur_commit = u64::from_be_bytes(item[commit_pos][1].clone().try_into()
|
||||
.map_err(|_|MizeError::new(105))?
|
||||
);
|
||||
|
||||
//because Update::change_whole_val trys to lock the itemstore as well
|
||||
drop(itemstore);
|
||||
|
||||
Reference in New Issue
Block a user