Add feature to convert anyhow errors to PHP exceptions (#110)

This commit is contained in:
David Cole 2021-11-23 18:03:25 +13:00 committed by GitHub
parent 6fd31621f2
commit c12dde1866
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 0 deletions

View File

@ -15,6 +15,7 @@ exclude = ["/.github", "/.crates", "/guide"]
bitflags = "1.2.1"
parking_lot = "0.11.2"
cfg-if = "1.0"
anyhow = { version = "1", optional = true }
ext-php-rs-derive = { version = "=0.7.1", path = "./crates/macros" }
[build-dependencies]

View File

@ -115,6 +115,15 @@ See the following links for the dependency crate requirements:
- [`cc`](https://github.com/alexcrichton/cc-rs#compile-time-requirements)
- [`bindgen`](https://rust-lang.github.io/rust-bindgen/requirements.html)
## Cargo Features
All features are disabled by default.
- `closure` - Enables the ability to return Rust closures to PHP. Creates a new
class type, `RustClosure`.
- `anyhow` - Implements `Into<PhpException>` for `anyhow::Error`, allowing you
to return anyhow results from PHP functions. Supports anyhow v1.x.
## Usage
This project only works for PHP >= 8.0 (for now). Due to the fact that the PHP

View File

@ -78,6 +78,13 @@ impl From<&str> for PhpException {
}
}
#[cfg(feature = "anyhow")]
impl From<anyhow::Error> for PhpException {
fn from(err: anyhow::Error) -> Self {
Self::new(err.to_string(), 0, crate::zend::ce::exception())
}
}
/// Throws an exception with a given message. See [`ClassEntry`] for some
/// built-in exception types.
///