Add custom errors with thiserror, log thread id
This commit is contained in:
59
src-tauri/src/errors.rs
Normal file
59
src-tauri/src/errors.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
use std::io;
|
||||
|
||||
use serde::Serialize;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug, Serialize)]
|
||||
#[serde(tag = "kind", content = "details")]
|
||||
pub enum SearchError {
|
||||
#[error("another thread found a solution")]
|
||||
SolutionFound,
|
||||
#[error("time limit exceeded")]
|
||||
Timeout,
|
||||
#[error("schedule is already manually filled")]
|
||||
ScheduleFull,
|
||||
#[error("user configuration is invalid")]
|
||||
Config(String),
|
||||
#[error("no solution found")]
|
||||
NoSolutionFound,
|
||||
}
|
||||
|
||||
impl From<anyhow::Error> for SearchError {
|
||||
fn from(err: anyhow::Error) -> Self {
|
||||
SearchError::Config(err.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum ExportError {
|
||||
#[error("path not found: {0}")]
|
||||
InvalidPath(#[from] io::Error),
|
||||
#[error("docx packaging error: {0}")]
|
||||
Packaging(String),
|
||||
#[error("failed to open doc: {0}")]
|
||||
OpenFailed(String),
|
||||
}
|
||||
|
||||
impl Serialize for ExportError {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
use serde::ser::SerializeStruct;
|
||||
|
||||
let mut s = serializer.serialize_struct("ExportError", 2)?;
|
||||
|
||||
s.serialize_field(
|
||||
"kind",
|
||||
match self {
|
||||
ExportError::InvalidPath(_) => "InvalidPath",
|
||||
ExportError::Packaging(_) => "Packaging",
|
||||
ExportError::OpenFailed(_) => "OpenFailed",
|
||||
},
|
||||
)?;
|
||||
|
||||
s.serialize_field("details", &self.to_string())?;
|
||||
|
||||
s.end()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user