Merge pull request #1275 from napi-rs/anyhow
feat(napi): `error_anyhow` feature
This commit is contained in:
commit
40d46f4b2a
4 changed files with 23 additions and 0 deletions
|
@ -1,4 +1,5 @@
|
||||||
[workspace]
|
[workspace]
|
||||||
|
resolver = "2"
|
||||||
members = [
|
members = [
|
||||||
"./crates/backend",
|
"./crates/backend",
|
||||||
"./crates/build",
|
"./crates/build",
|
||||||
|
|
|
@ -24,6 +24,7 @@ experimental = ["napi-sys/experimental"]
|
||||||
chrono_date = ["chrono", "napi5"]
|
chrono_date = ["chrono", "napi5"]
|
||||||
full = ["latin1", "napi8", "async", "serde-json", "experimental", "chrono_date"]
|
full = ["latin1", "napi8", "async", "serde-json", "experimental", "chrono_date"]
|
||||||
latin1 = ["encoding_rs"]
|
latin1 = ["encoding_rs"]
|
||||||
|
error_anyhow = ["anyhow"]
|
||||||
napi1 = []
|
napi1 = []
|
||||||
napi2 = ["napi1"]
|
napi2 = ["napi1"]
|
||||||
napi3 = ["napi2", "napi-sys/napi3"]
|
napi3 = ["napi2", "napi-sys/napi3"]
|
||||||
|
@ -53,6 +54,10 @@ once_cell = "1"
|
||||||
thread_local = "1"
|
thread_local = "1"
|
||||||
bitflags = "1"
|
bitflags = "1"
|
||||||
|
|
||||||
|
[dependencies.anyhow]
|
||||||
|
version = "1"
|
||||||
|
optional = true
|
||||||
|
|
||||||
[dependencies.napi-sys]
|
[dependencies.napi-sys]
|
||||||
version = "2.2.2"
|
version = "2.2.2"
|
||||||
path = "../sys"
|
path = "../sys"
|
||||||
|
|
|
@ -72,6 +72,13 @@ impl From<sys::napi_ref> for Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "anyhow")]
|
||||||
|
impl From<anyhow::Error> for Error {
|
||||||
|
fn from(value: anyhow::Error) -> Self {
|
||||||
|
Error::new(Status::GenericFailure, format!("{}", value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl fmt::Display for Error {
|
impl fmt::Display for Error {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
if !self.reason.is_empty() {
|
if !self.reason.is_empty() {
|
||||||
|
@ -160,6 +167,13 @@ impl TryFrom<sys::napi_extended_error_info> for ExtendedErrorInfo {
|
||||||
|
|
||||||
pub struct JsError(Error);
|
pub struct JsError(Error);
|
||||||
|
|
||||||
|
#[cfg(feature = "anyhow")]
|
||||||
|
impl From<anyhow::Error> for JsError {
|
||||||
|
fn from(value: anyhow::Error) -> Self {
|
||||||
|
JsError(Error::new(Status::GenericFailure, value.to_string()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct JsTypeError(Error);
|
pub struct JsTypeError(Error);
|
||||||
|
|
||||||
pub struct JsRangeError(Error);
|
pub struct JsRangeError(Error);
|
||||||
|
|
|
@ -217,3 +217,6 @@ pub mod __private {
|
||||||
|
|
||||||
#[cfg(feature = "tokio_rt")]
|
#[cfg(feature = "tokio_rt")]
|
||||||
pub extern crate tokio;
|
pub extern crate tokio;
|
||||||
|
|
||||||
|
#[cfg(feature = "error_anyhow")]
|
||||||
|
pub extern crate anyhow;
|
||||||
|
|
Loading…
Reference in a new issue