Skip to content
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
6 changes: 3 additions & 3 deletions examples/src/custom-layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl std::fmt::Display for MainError {
}
}

impl std::error::Error for MainError {}
impl Error for MainError {}

impl MainError {
/// Convert an `Exn<E>` into MainError with custom numbered list formatting.
Expand Down Expand Up @@ -92,7 +92,7 @@ mod app {
}
}

impl std::error::Error for AppError {}
impl Error for AppError {}
}

mod http {
Expand All @@ -115,7 +115,7 @@ mod http {
}
}

impl std::error::Error for HttpError {}
impl Error for HttpError {}
}

// Output when running `cargo run --example custom_layout`:
Expand Down
6 changes: 3 additions & 3 deletions examples/src/downcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ fn extract_http_status<E: Error + Send + Sync>(err: &Exn<E>) -> Option<u16> {
#[derive(Debug, Display)]
#[display("fatal error occurred in application")]
struct MainError;
impl std::error::Error for MainError {}
impl Error for MainError {}

mod app {
use super::*;
Expand All @@ -85,7 +85,7 @@ mod app {

#[derive(Debug, Display)]
pub struct AppError(String);
impl std::error::Error for AppError {}
impl Error for AppError {}
}

mod http {
Expand All @@ -104,7 +104,7 @@ mod http {
pub status: u16,
pub message: String,
}
impl std::error::Error for HttpError {}
impl Error for HttpError {}
}

// Output when running `cargo run --example downcast`:
Expand Down
53 changes: 53 additions & 0 deletions exn/src/ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2025 FastLabs Developers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::error::Error;

use crate::Exn;
use crate::Result;

/// Equivalent to `Ok::<_, Exn<E>>(value)`.
///
/// This simplifies creation of an `exn::Result` in places where type inference cannot deduce the
/// `E` type of the result &mdash; without needing to write `Ok::<_, Exn<E>>(value)`.
///
/// One might think that `exn::Result::Ok(value)` would work in such cases, but it does not.
///
/// ```console
/// error[E0282]: type annotations needed for `std::result::Result<i32, E>`
/// --> src/main.rs:11:13
/// |
/// 11 | let _ = exn::Result::Ok(1);
/// | - ^^^^^^^^^^^^^^^ cannot infer type for type parameter `E` declared on the enum `Result`
/// | |
/// | consider giving this pattern the explicit type `std::result::Result<i32, E>`, where the type parameter `E` is specified
/// ```
#[expect(non_snake_case)]
pub fn Ok<T, E: Error + Send + Sync + 'static>(value: T) -> Result<T, E> {
Result::Ok(value)
}

/// An extension trait for error types to raise them as exceptions.
pub trait ErrorExt: Error + Send + Sync + 'static {
/// Raise this error as a new exception.
#[track_caller]
fn raise(self) -> Exn<Self>
where
Self: Sized,
{
Exn::new(self)
}
}

impl<T> ErrorExt for T where T: Error + Send + Sync + 'static {}
24 changes: 3 additions & 21 deletions exn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,34 +77,16 @@

mod debug;
mod display;
mod ext;
mod impls;
mod macros;
mod option;
mod result;

pub use self::ext::ErrorExt;
pub use self::ext::Ok;
pub use self::impls::Exn;
pub use self::impls::Frame;
pub use self::option::OptionExt;
pub use self::result::Result;
pub use self::result::ResultExt;

/// Equivalent to `Ok::<_, Exn<E>>(value)`.
///
/// This simplifies creation of an `exn::Result` in places where type inference cannot deduce the
/// `E` type of the result &mdash; without needing to write `Ok::<_, Exn<E>>(value)`.
///
/// One might think that `exn::Result::Ok(value)` would work in such cases, but it does not.
///
/// ```console
/// error[E0282]: type annotations needed for `std::result::Result<i32, E>`
/// --> src/main.rs:11:13
/// |
/// 11 | let _ = exn::Result::Ok(1);
/// | - ^^^^^^^^^^^^^^^ cannot infer type for type parameter `E` declared on the enum `Result`
/// | |
/// | consider giving this pattern the explicit type `std::result::Result<i32, E>`, where the type parameter `E` is specified
/// ```
#[expect(non_snake_case)]
pub fn Ok<T, E: std::error::Error + Send + Sync + 'static>(value: T) -> Result<T, E> {
Result::Ok(value)
}
2 changes: 1 addition & 1 deletion exn/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub trait ResultExt {
type Success;

/// The `Err` type that would be wrapped in an [`Exn`].
type Error: Error + 'static;
type Error: Error + Send + Sync + 'static;

/// Raise a new exception on the [`Exn`] inside the [`Result`].
///
Expand Down
13 changes: 7 additions & 6 deletions exn/tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use exn::ErrorExt;
use exn::Exn;
use exn::OptionExt;
use exn::ResultExt;
Expand All @@ -29,7 +30,7 @@ impl std::error::Error for SimpleError {}

#[test]
fn test_error_straightforward() {
let e1 = Exn::new(SimpleError("E1"));
let e1 = SimpleError("E1").raise();
let e2 = e1.raise(SimpleError("E2"));
let e3 = e2.raise(SimpleError("E3"));
let e4 = e3.raise(SimpleError("E4"));
Expand All @@ -39,21 +40,21 @@ fn test_error_straightforward() {

#[test]
fn test_error_tree() {
let e1 = Exn::new(SimpleError("E1"));
let e1 = SimpleError("E1").raise();
let e3 = e1.raise(SimpleError("E3"));

let e9 = Exn::new(SimpleError("E9"));
let e9 = SimpleError("E9").raise();
let e10 = e9.raise(SimpleError("E10"));

let e11 = Exn::new(SimpleError("E11"));
let e11 = SimpleError("E11").raise();
let e12 = e11.raise(SimpleError("E12"));

let e5 = Exn::from_iter([e3, e10, e12], SimpleError("E5"));

let e2 = Exn::new(SimpleError("E2"));
let e2 = SimpleError("E2").raise();
let e4 = e2.raise(SimpleError("E4"));

let e7 = Exn::new(SimpleError("E7"));
let e7 = SimpleError("E7").raise();
let e8 = e7.raise(SimpleError("E8"));

let e6 = Exn::from_iter([e5, e4, e8], SimpleError("E6"));
Expand Down
2 changes: 1 addition & 1 deletion exn/tests/snapshots/simple__bail.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: exn/tests/simple.rs
expression: result.unwrap_err()
---
An error, at exn/tests/simple.rs:91:9
An error, at exn/tests/simple.rs:92:9
2 changes: 1 addition & 1 deletion exn/tests/snapshots/simple__ensure_fail.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: exn/tests/simple.rs
expression: result.unwrap_err()
---
An error, at exn/tests/simple.rs:111:9
An error, at exn/tests/simple.rs:112:9
10 changes: 5 additions & 5 deletions exn/tests/snapshots/simple__error_straightforward.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
source: exn/tests/simple.rs
expression: e5
---
E5, at exn/tests/simple.rs:36:17
E5, at exn/tests/simple.rs:37:17
|
|-> E4, at exn/tests/simple.rs:35:17
|-> E4, at exn/tests/simple.rs:36:17
|
|-> E3, at exn/tests/simple.rs:34:17
|-> E3, at exn/tests/simple.rs:35:17
|
|-> E2, at exn/tests/simple.rs:33:17
|-> E2, at exn/tests/simple.rs:34:17
|
|-> E1, at exn/tests/simple.rs:32:14
|-> E1, at exn/tests/simple.rs:33:32
24 changes: 12 additions & 12 deletions exn/tests/snapshots/simple__error_tree.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@
source: exn/tests/simple.rs
expression: e6
---
E6, at exn/tests/simple.rs:59:14
E6, at exn/tests/simple.rs:60:14
|
|-> E5, at exn/tests/simple.rs:51:14
|-> E5, at exn/tests/simple.rs:52:14
| |
| |-> E3, at exn/tests/simple.rs:43:17
| |-> E3, at exn/tests/simple.rs:44:17
| | |
| | |-> E1, at exn/tests/simple.rs:42:14
| | |-> E1, at exn/tests/simple.rs:43:32
| |
| |-> E10, at exn/tests/simple.rs:46:18
| |-> E10, at exn/tests/simple.rs:47:18
| | |
| | |-> E9, at exn/tests/simple.rs:45:14
| | |-> E9, at exn/tests/simple.rs:46:32
| |
| |-> E12, at exn/tests/simple.rs:49:19
| |-> E12, at exn/tests/simple.rs:50:19
| |
| |-> E11, at exn/tests/simple.rs:48:15
| |-> E11, at exn/tests/simple.rs:49:34
|
|-> E4, at exn/tests/simple.rs:54:17
|-> E4, at exn/tests/simple.rs:55:17
| |
| |-> E2, at exn/tests/simple.rs:53:14
| |-> E2, at exn/tests/simple.rs:54:32
|
|-> E8, at exn/tests/simple.rs:57:17
|-> E8, at exn/tests/simple.rs:58:17
|
|-> E7, at exn/tests/simple.rs:56:14
|-> E7, at exn/tests/simple.rs:57:32
2 changes: 1 addition & 1 deletion exn/tests/snapshots/simple__from_error.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: exn/tests/simple.rs
expression: result.unwrap_err()
---
An error, at exn/tests/simple.rs:80:9
An error, at exn/tests/simple.rs:81:9
2 changes: 1 addition & 1 deletion exn/tests/snapshots/simple__option_ext.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
source: exn/tests/simple.rs
expression: result.unwrap_err()
---
An error, at exn/tests/simple.rs:73:25
An error, at exn/tests/simple.rs:74:25
4 changes: 2 additions & 2 deletions exn/tests/snapshots/simple__result_ext.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
source: exn/tests/simple.rs
expression: result.unwrap_err()
---
Another error, at exn/tests/simple.rs:66:25
Another error, at exn/tests/simple.rs:67:25
|
|-> An error, at exn/tests/simple.rs:66:25
|-> An error, at exn/tests/simple.rs:67:25