1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//! Panic boundary — catches panics at the service boundary and returns a safe 500 response.
//!
//! Uses `std::panic::catch_unwind` (cross-platform). Does not rely on Unix signals.
use UnwindSafe;
/// A marker type representing the panic-safe layer.
///
/// Satisfies `Clone + Send + Sync + 'static` tower bounds for use as middleware.
;
/// Executes `f` inside a panic boundary.
///
/// Returns:
/// - `(200, "ok")` if `f` completes normally (the return value is discarded).
/// - `(500, json_body)` if `f` panics — the JSON body contains only `"internal_error"`,
/// never the panic message.
///
/// # Examples
///
/// ```
/// use secure_errors::panic::catch_panic_to_safe_response;
///
/// let (status, _body) = catch_panic_to_safe_response(|| "all good");
/// assert_eq!(status, 200);
///
/// let (status, body) = catch_panic_to_safe_response(|| panic!("boom"));
/// assert_eq!(status, 500);
/// assert!(body.contains("internal_error"));
/// ```