chore: migrate errors to thiserror

This commit is contained in:
zaaarf 2024-08-06 23:00:04 +02:00
parent 95ece68ae6
commit e85833a40f
No known key found for this signature in database
GPG key ID: 102E445F4C3F829B
2 changed files with 13 additions and 22 deletions

View file

@ -10,6 +10,7 @@ crate-type = ["cdylib"]
[dependencies] [dependencies]
# core # core
tracing = "0.1" tracing = "0.1"
thiserror = { version = "1.0.57" }
# woot # woot
codemp-woot = { git = "ssh://git@github.com/hexedtech/woot.git", features = ["serde"], tag = "v0.1.2" } codemp-woot = { git = "ssh://git@github.com/hexedtech/woot.git", features = ["serde"], tag = "v0.1.2" }
# proto # proto
@ -34,13 +35,9 @@ tracing-subscriber = { version = "0.3.18", optional = true }
# glue (java) # glue (java)
jni = { version = "0.21.1", features = ["invocation"], optional = true } jni = { version = "0.21.1", features = ["invocation"], optional = true }
jni-sys = { version = "0.3.0", optional = true } #jni-sys = { version = "0.3.0", optional = true }
rifgen = { git = "https://github.com/Kofituo/rifgen.git", rev = "d27d9785b2febcf5527f1deb6a846be5d583f7d7", optional = true }
log = { version = "0.4.21", optional = true }
# glue (lua) # glue (lua)
mlua = { version = "0.9.6", features = ["module", "luajit", "send"], optional = true } mlua = { version = "0.9.6", features = ["module", "luajit", "send"], optional = true }
thiserror = { version = "1.0.57", optional = true }
derive_more = { version = "0.99.17", optional = true } derive_more = { version = "0.99.17", optional = true }
# glue (js) # glue (js)
@ -64,8 +61,8 @@ pyo3-build-config = { version = "0.19.2", optional = true }
[features] [features]
default = [] default = []
lua = ["mlua", "thiserror", "derive_more", "lazy_static", "tracing-subscriber"] lua = ["mlua", "derive_more", "lazy_static", "tracing-subscriber"]
java = ["lazy_static", "jni", "jni-sys", "flapigen", "rifgen", "log"] java = ["lazy_static", "jni", "tracing-subscriber"]
java-artifact = ["java"] # also builds the jar java-artifact = ["java"] # also builds the jar
js = ["napi-build", "tracing-subscriber", "rmpv", "napi", "napi-derive", "futures"] js = ["napi-build", "tracing-subscriber", "rmpv", "napi", "napi-derive", "futures"]
python = ["pyo3", "pyo3-asyncio", "tracing-subscriber", "pyo3-build-config"] python = ["pyo3", "pyo3-asyncio", "tracing-subscriber", "pyo3-build-config"]

View file

@ -2,7 +2,7 @@
//! //!
//! library error helpers and types //! library error helpers and types
use std::{result::Result as StdResult, error::Error as StdError, fmt::Display}; use std::result::Result as StdResult;
use tracing::warn; use tracing::warn;
@ -45,36 +45,30 @@ pub type Result<T> = StdResult<T, Error>;
// TODO split this into specific errors for various parts of the library // TODO split this into specific errors for various parts of the library
/// codemp error type for library issues /// codemp error type for library issues
#[derive(Debug)] #[derive(Debug, thiserror::Error)]
pub enum Error { pub enum Error {
/// errors caused by tonic http layer /// errors caused by tonic http layer
#[error("tonic error (status: {status}, message: {message})")]
Transport { Transport {
status: String, status: String,
message: String, message: String,
}, },
/// errors caused by async channels /// errors caused by async channels
#[error("channel error, send: {send}")]
Channel { Channel {
send: bool send: bool
}, },
/// errors caused by wrong usage of library objects /// errors caused by wrong usage of library objects
#[error("invalid state error: {msg}")]
InvalidState { InvalidState {
msg: String, msg: String,
}, },
/// errors caused by wrong interlocking, safe to retry /// errors caused by wrong interlocking, safe to retry
Deadlocked, #[error("deadlocked error")]
} Deadlocked
impl StdError for Error {}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Transport { status, message } => write!(f, "Transport error: ({}) {}", status, message),
Self::Channel { send } => write!(f, "Channel error (send:{})", send),
_ => write!(f, "Unknown error"),
}
}
} }
impl From<tonic::Status> for Error { impl From<tonic::Status> for Error {