Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Report for user-friendly error output
- Loading branch information
1 parent
723cbc7
commit 81b942b
Showing
25 changed files
with
875 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use std::process::ExitCode; | ||
|
||
#[snafu::report] | ||
const NOT_HERE: u8 = 42; | ||
|
||
#[snafu::report] | ||
fn cannot_add_report_macro_with_no_return_value() {} | ||
|
||
#[snafu::report] | ||
fn cannot_add_report_macro_with_non_result_return_value() -> ExitCode { | ||
ExitCode::SUCCESS | ||
} | ||
|
||
fn main() {} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
error: `#[snafu::report]` may only be used on functions | ||
--> tests/ui/report.rs:4:1 | ||
| | ||
4 | const NOT_HERE: u8 = 42; | ||
| ^^^^^ | ||
|
||
error[E0277]: the trait bound `(): __InternalExtractErrorType` is not satisfied | ||
--> tests/ui/report.rs:6:1 | ||
| | ||
6 | #[snafu::report] | ||
| ^^^^^^^^^^^^^^^^ the trait `__InternalExtractErrorType` is not implemented for `()` | ||
| | ||
= help: the trait `__InternalExtractErrorType` is implemented for `Result<T, E>` | ||
= note: this error originates in the attribute macro `snafu::report` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error[E0277]: the trait bound `ExitCode: __InternalExtractErrorType` is not satisfied | ||
--> tests/ui/report.rs:9:1 | ||
| | ||
9 | #[snafu::report] | ||
| ^^^^^^^^^^^^^^^^ the trait `__InternalExtractErrorType` is not implemented for `ExitCode` | ||
| | ||
= help: the trait `__InternalExtractErrorType` is implemented for `Result<T, E>` | ||
= note: this error originates in the attribute macro `snafu::report` (in Nightly builds, run with -Z macro-backtrace for more info) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "report-provider-api" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
snafu = { path = "../..", features = ["backtraces-impl-std", "unstable-provider-api"] } |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
nightly |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#![cfg(test)] | ||
#![feature(error_generic_member_access, provide_any)] | ||
|
||
use snafu::{prelude::*, Report}; | ||
use std::process::ExitCode; | ||
|
||
#[test] | ||
fn provided_exit_code_is_returned() { | ||
use std::process::Termination; | ||
|
||
#[derive(Debug, Snafu)] | ||
enum TwoKindError { | ||
#[snafu(provide(ExitCode => ExitCode::from(2)))] | ||
Mild, | ||
#[snafu(provide(ExitCode => ExitCode::from(3)))] | ||
Extreme, | ||
} | ||
|
||
let mild = Report::from_error(MildSnafu.build()).report(); | ||
let expected_mild = ExitCode::from(2); | ||
|
||
assert!( | ||
nasty_hack_exit_code_eq(mild, expected_mild), | ||
"Wanted {:?} but got {:?}", | ||
expected_mild, | ||
mild, | ||
); | ||
|
||
let extreme = Report::from_error(ExtremeSnafu.build()).report(); | ||
let expected_extreme = ExitCode::from(3); | ||
|
||
assert!( | ||
nasty_hack_exit_code_eq(extreme, expected_extreme), | ||
"Wanted {:?} but got {:?}", | ||
expected_extreme, | ||
extreme, | ||
); | ||
} | ||
|
||
#[test] | ||
fn provided_backtrace_is_printed() { | ||
#[derive(Debug, Snafu)] | ||
struct Error { | ||
backtrace: snafu::Backtrace, | ||
} | ||
|
||
let r = Report::from_error(Snafu.build()); | ||
let msg = r.to_string(); | ||
|
||
let this_function = "::provided_backtrace_is_printed"; | ||
assert!( | ||
msg.contains(this_function), | ||
"Expected {msg:?} to contain {this_function:?}" | ||
); | ||
} | ||
|
||
fn nasty_hack_exit_code_eq(left: ExitCode, right: ExitCode) -> bool { | ||
use std::mem; | ||
|
||
let (left, right): (u8, u8) = unsafe { | ||
assert_eq!(mem::size_of::<u8>(), mem::size_of::<ExitCode>()); | ||
(mem::transmute(left), mem::transmute(right)) | ||
}; | ||
|
||
left == right | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "report-try-trait" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
snafu = { path = "../..", features = ["unstable-try-trait"] } |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
nightly |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#![cfg(test)] | ||
#![feature(try_trait_v2)] | ||
|
||
use snafu::{prelude::*, Report}; | ||
|
||
#[test] | ||
fn can_be_used_with_the_try_operator() { | ||
#[derive(Debug, Snafu)] | ||
struct ExampleError; | ||
|
||
fn mainlike() -> Report<ExampleError> { | ||
ExampleSnafu.fail()?; | ||
|
||
Report::ok() | ||
} | ||
|
||
let _: Report<ExampleError> = mainlike(); | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "v1_61" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
snafu = { path = "../..", features = ["rust_1_61"] } |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1.61 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#![cfg(test)] | ||
|
||
use snafu::{prelude::*, Report}; | ||
use std::process::ExitCode; | ||
|
||
#[test] | ||
fn termination_returns_failure_code() { | ||
use std::process::Termination; | ||
|
||
#[derive(Debug, Snafu)] | ||
struct Error; | ||
|
||
let r = Report::from_error(Error); | ||
let code: ExitCode = r.report(); | ||
|
||
assert!( | ||
nasty_hack_exit_code_eq(code, ExitCode::FAILURE), | ||
"Wanted {:?} but got {:?}", | ||
ExitCode::FAILURE, | ||
code, | ||
); | ||
} | ||
|
||
#[test] | ||
fn procedural_macro_works_with_result_return_type() { | ||
#[derive(Debug, Snafu)] | ||
struct Error; | ||
|
||
#[snafu::report] | ||
fn mainlike_result() -> Result<(), Error> { | ||
Ok(()) | ||
} | ||
|
||
let _: Report<Error> = mainlike_result(); | ||
} | ||
|
||
fn nasty_hack_exit_code_eq(left: ExitCode, right: ExitCode) -> bool { | ||
use std::mem; | ||
|
||
let (left, right): (u8, u8) = unsafe { | ||
assert_eq!(mem::size_of::<u8>(), mem::size_of::<ExitCode>()); | ||
(mem::transmute(left), mem::transmute(right)) | ||
}; | ||
|
||
left == right | ||
} |
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
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
Oops, something went wrong.