Crate panic_message[][src]

Expand description

A simple utility to take panic payloads (of type Box<dyn Any + Send + 'static>), primarily obtained from std::panic::catch_unwind, and converting them into messages (&str’s)

There are two primary entrypoints:

panic_message returns a &str and will do its best attempt to unpack a &str message from the payload, but will default to the literal "Box<dyn Any>" in an attempt to recreate what rustc does by default:

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 is similar, but returns an Option<&str>, returning None when it can’t unpack a message from the payload

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);

Note

This library takes in &Box<dyn Any + Send + 'static>. This is to make it clear that its borrowing from a full panic payload. This is to avoid misuse from:

  • Passing some other &dyn Any value
  • Coercion issues from Box<dyn Any to a &dyn Any that represents the Box itself

Functions

Attempt to produce a &str message from a panic payload. See module docs for usage.

Produce a &str message from a panic payload, with a default message. See module docs for usage.