rama-error 0.3.0-rc1

error types and utilities for rama
Documentation
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
use core::fmt::{self, Write as _};

use crate::{
    BoxError,
    std::{Box, String},
};

pub(super) struct ErrorWithContext {
    source: BoxError,
    fields: Option<crate::std::Vec<ContextField>>,
}

impl ErrorWithContext {
    pub(super) fn new(source: BoxError) -> Self {
        Self {
            source,
            fields: None,
        }
    }

    pub(super) fn insert_value<T>(&mut self, value: T)
    where
        T: fmt::Debug + fmt::Display + Send + Sync + 'static,
    {
        self.fields.get_or_insert_default().push(ContextField {
            key: None,
            value: Box::new(value),
        });
    }

    pub(super) fn insert_key_value<T>(&mut self, key: &'static str, value: T)
    where
        T: fmt::Debug + fmt::Display + Send + Sync + 'static,
    {
        let key = key.trim();
        if key.is_empty() {
            self.insert_value(value);
        } else {
            self.fields.get_or_insert_default().push(ContextField {
                key: Some(key),
                value: Box::new(value),
            });
        }
    }

    #[inline(always)]
    pub(super) fn insert_key_value_str<T>(&mut self, key: &'static str, value: T)
    where
        T: Into<String>,
    {
        let str = value.into();
        self.insert_key_value(key, str);
    }
}

trait ContextValue: fmt::Debug + fmt::Display + Send + Sync + 'static {}
impl<T: ?Sized + fmt::Debug + fmt::Display + Send + Sync + 'static> ContextValue for T {}

type BoxContextValue = Box<dyn ContextValue>;

#[derive(Debug)]
struct ContextField {
    key: Option<&'static str>,
    value: BoxContextValue,
}

struct DisplayAsDebug<'a>(&'a dyn fmt::Display);

impl<'a> fmt::Debug for DisplayAsDebug<'a> {
    #[inline(always)]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self.0, f)
    }
}

pub(super) struct DebugContextValue<T: fmt::Debug + Send + Sync + 'static>(pub(super) T);

impl<T: fmt::Debug + Send + Sync + 'static> fmt::Debug for DebugContextValue<T> {
    #[inline(always)]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&self.0, f)
    }
}

impl<T: fmt::Debug + Send + Sync + 'static> fmt::Display for DebugContextValue<T> {
    #[inline(always)]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&self.0, f)
    }
}

pub(super) struct HexContextValue<T: fmt::Debug + Send + Sync + 'static>(pub(super) T);

impl<T: fmt::Debug + Send + Sync + 'static> fmt::Debug for HexContextValue<T> {
    #[inline(always)]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:x?}", self.0)
    }
}

impl<T: fmt::Debug + Send + Sync + 'static> fmt::Display for HexContextValue<T> {
    #[inline(always)]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self, f)
    }
}

struct LogfmtEscaper<'a, 'b> {
    f: &'a mut fmt::Formatter<'b>,
}

impl<'a, 'b> fmt::Write for LogfmtEscaper<'a, 'b> {
    fn write_str(&mut self, s: &str) -> fmt::Result {
        // Emit runs of "clean" characters in a single write_str, only breaking
        // out for characters that need escaping. This keeps formatter overhead
        // low while ensuring control characters (incl. ANSI ESC \x1b and other
        // C0/C1 controls) are neutralized to prevent terminal-escape / log
        // injection when error context contains attacker-controlled data.
        let mut rest = s;
        loop {
            let mut found = None;
            for (i, ch) in rest.char_indices() {
                if needs_escape(ch) {
                    found = Some((i, ch));
                    break;
                }
            }
            let Some((i, ch)) = found else {
                if !rest.is_empty() {
                    self.f.write_str(rest)?;
                }
                return Ok(());
            };
            if i > 0 {
                self.f.write_str(&rest[..i])?;
            }
            write_escaped(self.f, ch)?;
            rest = &rest[i + ch.len_utf8()..];
        }
    }
}

#[inline]
fn needs_escape(ch: char) -> bool {
    // Backslash and quote are the logfmt structural escapes; everything that
    // is a Unicode control (C0, DEL, C1) is escaped to avoid terminal-escape
    // injection and to keep log records single-line and printable.
    matches!(ch, '\\' | '"') || ch.is_control()
}

fn write_escaped(f: &mut fmt::Formatter<'_>, ch: char) -> fmt::Result {
    match ch {
        '\\' => f.write_str("\\\\"),
        '"' => f.write_str("\\\""),
        '\n' => f.write_str("\\n"),
        '\r' => f.write_str("\\r"),
        '\t' => f.write_str("\\t"),
        c => write!(f, "\\u{{{:x}}}", c as u32),
    }
}

fn write_logfmt_display_value_always_quoted(
    f: &mut fmt::Formatter<'_>,
    v: &dyn fmt::Display,
) -> fmt::Result {
    f.write_str("\"")?;
    {
        let mut esc = LogfmtEscaper { f };
        write!(&mut esc, "{v}")?;
    }
    f.write_str("\"")
}

impl fmt::Display for ContextField {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.key {
            Some(key) => {
                write!(f, "{key}=")?;
                write_logfmt_display_value_always_quoted(f, self.value.as_ref())
            }
            None => write_logfmt_display_value_always_quoted(f, self.value.as_ref()),
        }
    }
}

struct DebugFields<'a>(&'a [ContextField]);

impl fmt::Debug for DebugFields<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut map = f.debug_map();
        for field in self.0 {
            match field.key {
                Some(key) => map.entry(&key, &DisplayAsDebug(field.value.as_ref())),
                None => map.entry(&"<none>", &DisplayAsDebug(field.value.as_ref())),
            };
        }
        map.finish()
    }
}

impl fmt::Debug for ErrorWithContext {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut ds = f.debug_struct("ErrorWithContext");
        ds.field("source", &self.source);

        if let Some(fields) = self.fields.as_ref().filter(|v| !v.is_empty()) {
            ds.field("fields", &DebugFields(fields.as_slice()));
        } else {
            ds.field("fields", &None::<()>);
        }

        ds.finish()
    }
}

impl ErrorWithContext {
    fn fmt_inline_fields(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(fields) = self.fields.as_ref().filter(|v| !v.is_empty()) {
            f.write_str(" | ")?;
            let mut fields_iter = fields.iter();
            if let Some(field) = fields_iter.next() {
                write!(f, "{field}")?;
            }
            for field in fields_iter {
                write!(f, " {field}")?;
            }
        }
        Ok(())
    }
}

impl fmt::Display for ErrorWithContext {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if !f.alternate() {
            write!(f, "{}", self.source)?;
            self.fmt_inline_fields(f)?;
            return Ok(());
        }

        writeln!(f, "{}", self.source)?;
        if let Some(fields) = self.fields.as_ref().filter(|v| !v.is_empty()) {
            writeln!(f, "Context:")?;
            for field in fields {
                writeln!(f, "  {field}")?;
            }
        }

        // Cap the cause-chain walk so a malformed Error impl that returns a
        // cycle from .source() cannot produce an unbounded loop here.
        const MAX_CAUSE_DEPTH: usize = 64;
        let mut idx = 0usize;
        let mut cur = self.source.as_ref().source();
        if cur.is_some() {
            writeln!(f, "Caused by:")?;
        }
        while let Some(err) = cur {
            if idx >= MAX_CAUSE_DEPTH {
                writeln!(f, "  ... (truncated)")?;
                break;
            }
            writeln!(f, "  {idx}: {err}")?;
            idx += 1;
            cur = err.source();
        }

        Ok(())
    }
}

impl crate::StdError for ErrorWithContext {
    fn source(&self) -> Option<&(dyn crate::StdError + 'static)> {
        Some(self.source.as_ref())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::{BoxErrorExt, ErrorExt, StdError};

    #[derive(Debug, Clone)]
    struct BoomError;

    impl fmt::Display for BoomError {
        fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
            write!(f, "boom")
        }
    }

    impl core::error::Error for BoomError {}

    #[test]
    fn display_non_alternate_without_fields_is_source_only() {
        let src = BoomError;
        let err = ErrorWithContext::new(BoxError::from(src));

        assert_eq!(format!("{err}"), "boom");
    }

    #[test]
    fn empty_keys_are_seen_as_simple_values_and_keys_are_trimmed() {
        let err = BoxError::from_static_str("test error")
            .context_field("foo  ", "bar")
            .context_field("  ", "baz")
            .context_field("", 42);

        assert_eq!(format!("{err}"), "test error | foo=\"bar\" \"baz\" \"42\"");
    }

    #[test]
    fn display_non_alternate_with_fields_is_single_line_logfmt() {
        let src = BoomError;
        let mut err = ErrorWithContext::new(BoxError::from(src));

        err.insert_key_value("path", "/a,b/c");
        err.insert_key_value("note", "hello \"world\"\nnext");
        err.insert_value("bare,value");

        let s = format!("{err}");

        // Source first
        assert!(s.starts_with("boom"), "got: {s:?}");

        // Inline context separator
        assert!(s.contains(" | "), "got: {s:?}");

        // Always quoted key/value
        assert!(
            s.contains(r#"path="/a,b/c""#),
            "expected quoted path, got: {s:?}"
        );

        // Escaping for quotes and newline
        assert!(
            s.contains(r#"note="hello \"world\"\nnext""#),
            "expected escaped note, got: {s:?}"
        );

        // Unkeyed value uses quotes too
        assert!(
            s.contains(r#""bare,value""#),
            "expected quoted unkeyed value, got: {s:?}"
        );

        // Fields are separated by spaces, not commas
        assert!(!s.contains(", "), "unexpected comma delimiter, got: {s:?}");
    }

    #[test]
    fn display_alternate_includes_context_block_when_fields_exist() {
        let src = BoomError;
        let mut err = ErrorWithContext::new(BoxError::from(src));

        err.insert_key_value("path", "/a,b/c");
        err.insert_value("bare");

        let s = format!("{err:#}");

        // Prints source on its own line
        assert!(s.starts_with("boom\n"), "got: {s:?}");

        // Context section
        assert!(s.contains("Context:\n"), "got: {s:?}");

        // Indented entries
        assert!(
            s.contains(r#"  path="/a,b/c""#),
            "expected indented kv entry, got: {s:?}"
        );
        assert!(
            s.contains(r#"  "bare""#),
            "expected indented unkeyed entry, got: {s:?}"
        );
    }

    #[test]
    fn display_alternate_prints_cause_chain_when_source_has_source() {
        #[derive(Debug)]
        struct Outer {
            inner: BoomError,
        }

        impl core::fmt::Display for Outer {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                write!(f, "outer")
            }
        }

        impl StdError for Outer {
            fn source(&self) -> Option<&(dyn StdError + 'static)> {
                Some(&self.inner)
            }
        }

        let src = Outer { inner: BoomError };

        let mut err = ErrorWithContext::new(BoxError::from(src));
        err.insert_key_value("k", "v");

        let s = format!("{err:#}");

        // We should see the cause chain section and indexed causes.
        assert!(s.contains("Caused by:\n"), "got: {s:?}");
        assert!(s.contains("  0: boom\n"), "got: {s:?}");
    }

    #[test]
    fn debug_includes_type_name_and_fields_map() {
        let src = BoomError;
        let mut err = ErrorWithContext::new(BoxError::from(src));
        err.insert_key_value("path", "/a,b/c");
        err.insert_value("bare");

        let s = format!("{err:?}");

        assert!(s.contains("ErrorWithContext"), "got: {s:?}");
        assert!(s.contains("source"), "got: {s:?}");
        assert!(s.contains("fields"), "got: {s:?}");

        // DebugFields prints a debug_map with keys; unkeyed uses "<none>"
        assert!(s.contains("path"), "got: {s:?}");
        assert!(s.contains("<none>"), "got: {s:?}");
    }

    #[test]
    fn source_returns_inner_error() {
        let src = BoomError;
        let err = ErrorWithContext::new(BoxError::from(src));

        let src_ref = err.source().expect("source should exist");
        assert_eq!(src_ref.to_string(), "boom");
    }

    #[test]
    fn logfmt_escapes_ansi_and_control_chars() {
        // ANSI ESC (\x1b), NUL, DEL, BEL, C1 CSI — none of these may appear
        // literally in formatted output, to prevent terminal-escape / log
        // injection when error context contains attacker-controlled bytes.
        let src = BoomError;
        let mut err = ErrorWithContext::new(BoxError::from(src));

        err.insert_key_value("ansi", "\x1b[31mred\x1b[0m");
        err.insert_key_value("nul", "a\x00b");
        err.insert_key_value("del", "a\x7fb");
        err.insert_key_value("bel", "a\x07b");
        err.insert_key_value("c1", "a\u{9b}b");

        let s = format!("{err}");

        // No literal control bytes leaked through.
        for bad in ['\x00', '\x07', '\x1b', '\x7f', '\u{9b}'] {
            assert!(
                !s.contains(bad),
                "raw control char {bad:?} leaked into output: {s:?}"
            );
        }

        // Escaped forms are present.
        assert!(s.contains(r"\u{1b}"), "got: {s:?}");
        assert!(s.contains(r"\u{0}"), "got: {s:?}");
        assert!(s.contains(r"\u{7f}"), "got: {s:?}");
        assert!(s.contains(r"\u{7}"), "got: {s:?}");
        assert!(s.contains(r"\u{9b}"), "got: {s:?}");

        // Existing structural escapes still work.
        let src2 = BoomError;
        let mut err2 = ErrorWithContext::new(BoxError::from(src2));
        err2.insert_key_value("q", "a\"b\\c");
        let s2 = format!("{err2}");
        assert!(s2.contains(r#"q="a\"b\\c""#), "got: {s2:?}");
    }

    #[test]
    fn cause_chain_is_capped_to_avoid_unbounded_loops() {
        // A malicious Error impl can return a cycle from .source(); make sure
        // alternate Display still terminates.
        struct Cycle;
        impl fmt::Debug for Cycle {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.write_str("cycle")
            }
        }
        impl fmt::Display for Cycle {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.write_str("cycle")
            }
        }
        impl StdError for Cycle {
            fn source(&self) -> Option<&(dyn StdError + 'static)> {
                // Self-cycle: returns itself as its own source.
                Some(self)
            }
        }

        let err = ErrorWithContext::new(Box::new(Cycle));
        let s = format!("{err:#}");
        assert!(s.contains("(truncated)"), "got: {s:?}");
    }
}