Skip to content

Commit c12dde1

Browse files
Add feature to convert anyhow errors to PHP exceptions (#110)
1 parent 6fd3162 commit c12dde1

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ exclude = ["/.github", "/.crates", "/guide"]
1515
bitflags = "1.2.1"
1616
parking_lot = "0.11.2"
1717
cfg-if = "1.0"
18+
anyhow = { version = "1", optional = true }
1819
ext-php-rs-derive = { version = "=0.7.1", path = "./crates/macros" }
1920

2021
[build-dependencies]

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ See the following links for the dependency crate requirements:
115115
- [`cc`](https://github.com/alexcrichton/cc-rs#compile-time-requirements)
116116
- [`bindgen`](https://rust-lang.github.io/rust-bindgen/requirements.html)
117117

118+
## Cargo Features
119+
120+
All features are disabled by default.
121+
122+
- `closure` - Enables the ability to return Rust closures to PHP. Creates a new
123+
class type, `RustClosure`.
124+
- `anyhow` - Implements `Into<PhpException>` for `anyhow::Error`, allowing you
125+
to return anyhow results from PHP functions. Supports anyhow v1.x.
126+
118127
## Usage
119128

120129
This project only works for PHP >= 8.0 (for now). Due to the fact that the PHP

src/exception.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ impl From<&str> for PhpException {
7878
}
7979
}
8080

81+
#[cfg(feature = "anyhow")]
82+
impl From<anyhow::Error> for PhpException {
83+
fn from(err: anyhow::Error) -> Self {
84+
Self::new(err.to_string(), 0, crate::zend::ce::exception())
85+
}
86+
}
87+
8188
/// Throws an exception with a given message. See [`ClassEntry`] for some
8289
/// built-in exception types.
8390
///

0 commit comments

Comments
 (0)