Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature to convert anyhow errors to PHP exceptions #110

Merged
merged 1 commit into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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