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]
# core
tracing = "0.1"
thiserror = { version = "1.0.57" }
# woot
codemp-woot = { git = "ssh://git@github.com/hexedtech/woot.git", features = ["serde"], tag = "v0.1.2" }
# proto
@ -34,13 +35,9 @@ tracing-subscriber = { version = "0.3.18", optional = true }
# glue (java)
jni = { version = "0.21.1", features = ["invocation"], 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 }
#jni-sys = { version = "0.3.0", optional = true }
# glue (lua)
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 }
# glue (js)
@ -64,8 +61,8 @@ pyo3-build-config = { version = "0.19.2", optional = true }
[features]
default = []
lua = ["mlua", "thiserror", "derive_more", "lazy_static", "tracing-subscriber"]
java = ["lazy_static", "jni", "jni-sys", "flapigen", "rifgen", "log"]
lua = ["mlua", "derive_more", "lazy_static", "tracing-subscriber"]
java = ["lazy_static", "jni", "tracing-subscriber"]
java-artifact = ["java"] # also builds the jar
js = ["napi-build", "tracing-subscriber", "rmpv", "napi", "napi-derive", "futures"]
python = ["pyo3", "pyo3-asyncio", "tracing-subscriber", "pyo3-build-config"]

View file

@ -2,7 +2,7 @@
//!
//! 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;
@ -45,36 +45,30 @@ pub type Result<T> = StdResult<T, Error>;
// TODO split this into specific errors for various parts of the library
/// codemp error type for library issues
#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// errors caused by tonic http layer
#[error("tonic error (status: {status}, message: {message})")]
Transport {
status: String,
message: String,
},
/// errors caused by async channels
#[error("channel error, send: {send}")]
Channel {
send: bool
},
/// errors caused by wrong usage of library objects
#[error("invalid state error: {msg}")]
InvalidState {
msg: String,
},
/// errors caused by wrong interlocking, safe to retry
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"),
}
}
#[error("deadlocked error")]
Deadlocked
}
impl From<tonic::Status> for Error {