finch/
errors.rs

1use std::result::Result as StdResult;
2
3use thiserror::Error;
4
5#[derive(Error, Debug)]
6pub enum FinchError {
7    #[error("failed to load/read/write file: {0:?}")]
8    Io(#[from] std::io::Error),
9    #[error("capnproto error: {0:?}")]
10    Capnproto(#[from] capnp::Error),
11    #[error("failed to parse the fasta/fastq file: {0}")]
12    Needletail(#[from] needletail::errors::ParseError),
13    #[error("failed to parse as integer")]
14    IntError(#[from] core::num::ParseIntError),
15    #[error("failed to parse as float")]
16    FloatError(#[from] core::num::ParseFloatError),
17    #[error("enum value not found in schema")]
18    SchemaError(#[from] capnp::NotInSchema),
19    #[error("json error: {0:?}")]
20    Json(#[from] serde_json::Error),
21    #[error("Finch error: {0}")]
22    Message(String),
23}
24
25pub type FinchResult<T> = StdResult<T, FinchError>;
26
27// TODO: remove the macro magic if possible when done with moving off failure
28
29#[doc(hidden)]
30#[macro_export]
31macro_rules! bail {
32    ($e:expr) => {
33        return Err($crate::errors::FinchError::Message($e.to_owned()));
34    };
35    ($fmt:expr, $($arg:tt)*) => {
36        return Err($crate::errors::FinchError::Message(format!($fmt, $($arg)*)));
37    };
38}
39
40#[doc(hidden)]
41#[macro_export]
42macro_rules! format_err {
43    ($($arg:tt)*) => { $crate::errors::FinchError::Message(format!($($arg)*)) }
44}