feat(napi): error_anyhow feature

This commit is contained in:
iuser 2022-08-14 08:03:31 +08:00 committed by LongYinan
parent c4eb51b509
commit 13996c1864
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
4 changed files with 23 additions and 0 deletions

View file

@ -1,4 +1,5 @@
[workspace]
resolver = "2"
members = [
"./crates/backend",
"./crates/build",

View file

@ -24,6 +24,7 @@ experimental = ["napi-sys/experimental"]
chrono_date = ["chrono", "napi5"]
full = ["latin1", "napi8", "async", "serde-json", "experimental", "chrono_date"]
latin1 = ["encoding_rs"]
error_anyhow = ["anyhow"]
napi1 = []
napi2 = ["napi1"]
napi3 = ["napi2", "napi-sys/napi3"]
@ -53,6 +54,10 @@ once_cell = "1"
thread_local = "1"
bitflags = "1"
[dependencies.anyhow]
version = "1"
optional = true
[dependencies.napi-sys]
version = "2.2.2"
path = "../sys"

View file

@ -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 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if !self.reason.is_empty() {
@ -160,6 +167,13 @@ impl TryFrom<sys::napi_extended_error_info> for ExtendedErrorInfo {
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 JsRangeError(Error);

View file

@ -217,3 +217,6 @@ pub mod __private {
#[cfg(feature = "tokio_rt")]
pub extern crate tokio;
#[cfg(feature = "error_anyhow")]
pub extern crate anyhow;