Created
August 29, 2024 09:20
-
-
Save Porges/34a51e4f5fc707a51c0624ec37aa11d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(error_generic_member_access)] // for provide API | |
#![feature(try_trait_v2)] // for MainResult to use ? syntax | |
use errful::{Error, MainResult, Span}; | |
#[derive(Debug, Error)] | |
#[error( | |
display = "something unexpected happened", | |
exit_code = 123, // if this is returned from `main` | |
url = "https://example.com/my-error", | |
code = "MY_ERROR", | |
severity = errful::Severity::Warning, | |
)] | |
struct MyError { | |
#[error(source)] | |
inner: std::num::ParseIntError, // any std::error::Error will do | |
#[error(source_code)] | |
input: String, // the input which caused the error | |
#[error(label = "this should be a number")] | |
whole_location: Span<u8>, // label a location within the input | |
#[error(label = inner)] // can also use an error field as a label | |
error_location: Span<u8>, | |
} | |
fn main() -> MainResult<MyError> { | |
failing_function()?; | |
MainResult::success() | |
} | |
fn failing_function() -> Result<(), MyError> { | |
let input = "123x5".to_string(); | |
let inner = input.parse::<i32>().unwrap_err(); | |
let err = MyError { | |
inner, | |
input, | |
error_location: Span::new(3.into(), 1.into()), | |
whole_location: Span::new(0.into(), 5.into()), | |
}; | |
Err(err) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment