-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow personalizing the panic message
Make setup_panic accept a function as a parameter, that will be called to write the message. This function receives a Write reference, an optional Path and the Metadata. If no function is provided, use the original function "write_msg"
- Loading branch information
1 parent
343f1c0
commit 89e7986
Showing
6 changed files
with
121 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
members = [ | ||
"tests/single-panic", | ||
"tests/custom-panic", | ||
"tests/custom-message", | ||
] | ||
resolver = "2" | ||
|
||
|
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,15 @@ | ||
[package] | ||
name = "custom-message-test" | ||
version = "0.1.0" | ||
authors = ["Human Panic Authors <[email protected]>"] | ||
edition.workspace = true | ||
publish = false | ||
|
||
[package.metadata.release] | ||
release = false | ||
|
||
[dependencies] | ||
human-panic = { path = "../.." } | ||
|
||
[dev-dependencies] | ||
snapbox = { version = "0.6.4", features = ["cmd"] } |
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 @@ | ||
use std::path::Path; | ||
|
||
use human_panic::metadata; | ||
use human_panic::Metadata; | ||
use human_panic::setup_panic; | ||
|
||
fn custom_message( | ||
buf: &mut dyn std::io::Write, | ||
path: Option<&Path>, | ||
meta: &Metadata | ||
) -> std::io::Result<()> { | ||
let Metadata { | ||
name, | ||
homepage, | ||
.. | ||
} = meta; | ||
|
||
writeln!(buf, "A fatal error ocurred!")?; | ||
writeln!(buf,"{name} is DEAD :(")?; | ||
writeln!( | ||
buf, | ||
"You can see details at \"{}\".", | ||
match path { | ||
Some(fp) => format!("{}", fp.display()), | ||
None => "<Failed to store file to disk>".to_owned(), | ||
}, | ||
)?; | ||
|
||
if let Some(homepage) = homepage { | ||
writeln!(buf, "\nShare your condolences: {homepage}")?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn main() { | ||
setup_panic!( | ||
metadata!() | ||
.authors("My Company Support <[email protected]>") | ||
.homepage("www.mycompany.com") | ||
.support("- Open a support request by email to [email protected]"), | ||
custom_message); | ||
|
||
println!("A normal log message"); | ||
panic!("OMG EVERYTHING IS ON FIRE!!!"); | ||
} |
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,29 @@ | ||
#[test] | ||
#[cfg_attr(debug_assertions, ignore)] | ||
fn release() { | ||
snapbox::cmd::Command::new(snapbox::cmd::cargo_bin!("custom-message-test")) | ||
.assert() | ||
.stderr_eq(snapbox::str![[r#" | ||
A fatal error ocurred! | ||
custom-message-test is DEAD :( | ||
You can see details at [..]. | ||
Share your condolences: www.mycompany.com | ||
"#]]) | ||
.code(101); | ||
} | ||
|
||
#[test] | ||
#[cfg_attr(not(debug_assertions), ignore)] | ||
fn debug() { | ||
snapbox::cmd::Command::new(snapbox::cmd::cargo_bin!("custom-message-test")) | ||
.assert() | ||
.stderr_eq(snapbox::str![[r#" | ||
thread 'main' panicked at tests/custom-message/src/main.rs:45:5: | ||
OMG EVERYTHING IS ON FIRE!!! | ||
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace | ||
"#]]) | ||
.code(101); | ||
} |