#panic #message #info #payload

panic-message

Get a panic message from a panic payload

2 unstable releases

0.3.0 Aug 27, 2021
0.2.0 Aug 26, 2021
Download history 7278/week @ 2022-11-28 6814/week @ 2022-12-05 7233/week @ 2022-12-12 4417/week @ 2022-12-19 2727/week @ 2022-12-26 4710/week @ 2023-01-02 8666/week @ 2023-01-09 8865/week @ 2023-01-16 10082/week @ 2023-01-23 10896/week @ 2023-01-30 5659/week @ 2023-02-06 3529/week @ 2023-02-13 2818/week @ 2023-02-20 2238/week @ 2023-02-27 2335/week @ 2023-03-06 2710/week @ 2023-03-13

10,209 downloads per month
Used in 36 crates (9 directly)

MIT/Apache

8KB
65 lines

Get a message from a panic payload

See the docs for more info.

License

This project is licensed under either of Apache License, Version 2.0, (LICENSE-APACHE or MIT license (LICENSE-MIT. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


lib.rs:

A simple utility to take panic payloads, primarily obtained from obtained from [std::panic::catch_unwind] or [std::panic::set_hook], and converting them into messages (&str's)

panic_message

[panic_message][crate::panic_message] takes a payload from [std::panic::catch_unwind] and returns a &str, doing its best attempt to unpack a &str message from the payload, defaulting to the literal "Box<dyn Any>" in an attempt to recreate what rustc does.

Examples

use std::panic::catch_unwind;

let payload = catch_unwind(|| {
    panic!("gus"); }).unwrap_err();

let msg = panic_message::panic_message(&payload);
assert_eq!("gus", msg);

Non-string payload:

use std::panic::catch_unwind;

let payload = catch_unwind(|| {
    std::panic::panic_any(1);
}).unwrap_err();

let msg = panic_message::panic_message(&payload);
assert_eq!("Box<dyn Any>", msg);

get_panic_message

[get_panic_message][crate::get_panic_message] is similar to panic_message, but returns an Option<&str>, returning None when it can't unpack a message from the payload

Examples

use std::panic::catch_unwind;

let payload = catch_unwind(|| {
    panic!("gus");
}).unwrap_err();

let msg = panic_message::get_panic_message(&payload);
assert_eq!(Some("gus"), msg);

Non-string payload:

use std::panic::catch_unwind;

let payload = catch_unwind(|| {
    std::panic::panic_any(1);
}).unwrap_err();

let msg = panic_message::get_panic_message(&payload);
assert_eq!(None, msg);

PanicInfo

This library also offers apis for getting messages from [PanicInfo][std::panic::PanicInfo]'s as returned by [std::panic::set_hook`]:

  • [panic_info_message][crate::panic_info_message] is similar to [panic_message][crate::panic_message] and has a default string "Box<dyn Any>"
  • [get_panic_info_message][crate::get_panic_info_message] is similar to [get_panic_message][crate::get_panic_message] and returns an Option<&str>

Example

std::panic::set_hook(Box::new(|pi| {
    println!("{}", panic_message::panic_info_message(pi));
    println!("{:?}", panic_message::get_panic_info_message(pi));
}));

Note

This library has methods that take values that are returned by standard mechanisms to obtain panic payloads, as opposed to a single generic method that takes &dyn Any. This is to prevent misuse. For example, the reason to take PanicInfo and not the &dyn Any as returned by [PanicInfo::payload][std::panic::PanicInfo::payload] is because Box<dyn Any> can be coerced into &dyn Any, which would make a method that takes &dyn Any possible to misuse with a payload from [std::panic::catch_unwind].

No runtime deps