Skip to content

Commit

Permalink
Add feature to convert anyhow errors to PHP exceptions (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcole1340 authored Nov 23, 2021
1 parent 6fd3162 commit c12dde1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/exception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down

0 comments on commit c12dde1

Please sign in to comment.