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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
//! # HTTP Problem-based Error Handling Library
//!
//! This crate provides a general mechanism for error handling based on the
//! [RFC 7807] problem entity with the [`Problem`] type.
//!
//! Users can find many pre-defined errors at the [`http`] and [`sql`] modules.
//!
//! The workflow for error handling with this library is as follow:
//!
//! 1. Use the predefined errors/functions or define a new one with the
//!    [`define_custom_type!`] macro to returns errors in functions that
//!    return [`Result<T, Problem>`] (an alias is provided in the library).
//!     * You can also use the extensions traits [`ResultExt`],
//!       [`ProblemResultExt`], [`OptionExt`] to handle common cases.
//! 2. Catch any desired error with [`ProblemResultExt::catch_err`].
//!
//! [RFC 7807]: https://tools.ietf.org/html/rfc7807
//! [`Problem`]: crate::Problem
//! [`define_custom_type!`]: crate::define_custom_type
//! [`http`]: crate::http
//! [`sql`]: crate::sql
//! [`Result<T, Problem>`]: crate::Result
//! [`ResultExt`]: crate::ext::ResultExt
//! [`ProblemResultExt`]: crate::ext::ProblemResultExt
//! [`OptionExt`]: crate::ext::OptionExt
//! [`ProblemResultExt::catch_err`]: crate::ext::ProblemResultExt::catch_err
#![feature(iter_intersperse)]
use std::{borrow::Cow, collections::HashMap, panic::Location};

use backtrace::Backtrace;
use eyre::EyreContext;
use parking_lot::Once;
use serde::ser::SerializeMap;

mod macros;

#[cfg(feature = "actix")]
mod actix;
#[cfg(feature = "axum")]
mod axum;
mod commons;
mod custom;
pub(crate) use self::custom::*;
mod ext;
/// HTTP related well-known errors.
pub mod http;
/// Definitions for global error reporting.
pub mod reporter;
use self::reporter::Report;
#[cfg(feature = "sql")]
/// SQL related well-known errors.
pub mod sql;

/// Prelude imports for this crate.
pub mod prelude {
    #[cfg(feature = "actix")]
    pub use super::actix::*;
    #[cfg(feature = "axum")]
    pub use super::axum::*;
    #[cfg(feature = "sql")]
    pub use super::sql::*;
    pub use super::{commons::*, custom::*, ext::*, http, Problem, Result};
}

pub(crate) fn blank_type_uri() -> custom::Uri {
    custom::Uri::from_static("about:blank")
}

/// An alias for a static `Cow<str>`.
pub type CowStr = Cow<'static, str>;

/// Convenience alias for functions that can error ouy with [`Problem`].
pub type Result<T, E = Problem> = std::result::Result<T, E>;

/// Install the necessary machinery to make [`Problem`] based
/// error handling work.
///
/// Ideally, this should should be called first in the main of the binary.
///
/// # Panics
///
/// This function panics if fails to set the necessary machinery,
/// which happens if the function was already called before.
pub fn install() {
    static HOOK_INSTALLED: Once = Once::new();

    HOOK_INSTALLED.call_once(|| {
        eyre::set_hook(Box::new(crate::reporter::capture_handler))
            .expect("Failed to set error hook, maybe install was already called?");
    })
}

/// A [RFC 7807] Problem Error.
///
/// # Error Cause
///
/// This type provides methods to access the inner error cause. Although we
/// store it, we DO NOT send it when serializing the problem, as it would
/// leak implementation details.
///
/// # Backtraces
///
/// Many implementations of the RFC add automatic backtrace to the problem.
/// This is NOT done by this type and MUST NOT be added manually, as exposing
/// the backtrace to the caller will expose implementation details and CAN
/// be source of vulnerabilities.
///
/// # Custom Problem Types
///
/// When an HTTP API needs to define a response that indicates an error
/// condition, it might be appropriate to do so by defining a new problem type.
///
/// New problem type definitions MUST document:
///
/// 1. a type URI (typically, with the "http" or "https" scheme),
/// 2. a title that appropriately describes it (think short), and
/// 3. the HTTP status code for it to be used with.
///
/// A problem type definition MAY specify additional members on the problem
/// details object. For example, an extension might use typed links [RFC 5988]
/// to another resource that can be used by machines to resolve the problem.
///
/// Avoid defining custom problem types, preferring to use standardized HTTP
/// status whenever possible. Custom types should only be defined if no
/// HTTP status code can properly encode the occurred problem. As an example:
///
/// ```ignore
/// {
///     "type": "https://example.com/probs/out-of-credit",
///     "status": 403,
///     "title": "You do not have enough credit",
///     "detail": "Your current balance is 30, but that costs 50",
///     "balance": 30,
///     "accounts": ["/account/12345", "/account/67890"]
/// }
/// ```
///
/// When adding a new problem type, we suggest that the type reference should
/// also be added to the main API gateway page.
///
/// # Error Instances
///
/// We currently do not track error instances (the `instance` field defined
/// in the RFC). This may change in the future.
///
/// [RFC 7807]: https://tools.ietf.org/html/rfc7807
/// [RFC 5988]: https://tools.ietf.org/html/rfc5988
#[derive(Default)]
pub struct Problem {
    inner: Box<ProblemInner>,
}

#[derive(Debug)]
struct ProblemInner {
    r#type: Uri,
    title: CowStr,
    status: StatusCode,
    details: CowStr,
    cause: eyre::Report,
    extensions: Extensions,
}

impl Default for ProblemInner {
    fn default() -> Self {
        Self {
            r#type: blank_type_uri(),
            title: Cow::Borrowed(""),
            status: StatusCode::default(),
            details: Cow::Borrowed(""),
            cause: eyre::Report::msg(""),
            extensions: Extensions::default(),
        }
    }
}

impl ProblemInner {
    fn report(&self) -> &Report {
        self.cause
            .handler()
            .downcast_ref::<Report>()
            .expect("Problem used without installation")
    }
}

impl serde::Serialize for Problem {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut map = serializer.serialize_map(None)?;

        map.serialize_entry(&"status", &self.status().as_u16())?;

        if !matches!(self.type_().scheme_str(), None | Some("about")) {
            map.serialize_entry(&"type", &format_args!("{}", self.type_()))?;
        }

        map.serialize_entry(&"title", &self.title())?;
        map.serialize_entry(&"detail", &self.details())?;

        for (k, v) in &self.extensions().inner {
            map.serialize_entry(k, v)?;
        }

        map.end()
    }
}

impl std::fmt::Debug for Problem {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.inner.report().debug(self.cause(), f)
    }
}

impl std::fmt::Display for Problem {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        use eyre::EyreHandler;

        writeln!(
            f,
            "{} - {}: {}",
            self.status(),
            self.title(),
            self.details()
        )?;
        self.inner.report().display(&*self.inner.cause, f)?;

        Ok(())
    }
}

impl std::error::Error for Problem {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        Some(self.cause())
    }
}

impl Problem {
    pub(crate) fn report_as_error(&self) {
        if let Some(reporter) = self::reporter::global_reporter() {
            if reporter.should_report_error(self) {
                reporter.report_error(self);
            }
        }
    }
}

/// [`Problem`] constructors.
impl Problem {
    /// Create a custom problem from the given type.
    ///
    /// See the type's documentation for more information about custom types.
    #[track_caller]
    pub fn custom(status: StatusCode, r#type: Uri) -> Self {
        let mut problem = Self::from_status(status);
        problem.inner.r#type = r#type;
        problem
    }

    /// Create a new problem for the given status code.
    #[track_caller]
    pub fn from_status(status: StatusCode) -> Self {
        install();

        let title = status.canonical_reason().unwrap();
        Self {
            inner: Box::new(ProblemInner {
                title: title.into(),
                cause: eyre::Report::msg(title),
                status,
                ..ProblemInner::default()
            }),
        }
    }

    /// Sets the title for this problem.
    ///
    /// **OBS**: HTTP Status only problems MUST NOT have their title changed.
    ///
    /// This method doesn't protect against this, be sure to follow the spec.
    #[must_use]
    pub fn with_title(mut self, title: impl Into<CowStr>) -> Self {
        self.inner.title = title.into();
        self
    }

    /// Sets the detail for this problem.
    #[must_use]
    pub fn with_detail(mut self, detail: impl Into<CowStr>) -> Self {
        self.inner.details = detail.into();
        self
    }

    /// Sets the error cause for this problem.
    #[must_use]
    #[track_caller]
    pub fn with_cause<E>(mut self, cause: E) -> Self
    where
        E: std::error::Error + Send + Sync + 'static,
    {
        self.inner.cause = eyre::Report::new(cause);
        self
    }

    /// Add a new extension value for the problem.
    ///
    /// The `telemetry` extension is reserved for internal use and the `cause`
    /// extension is reserved for future use.
    ///
    /// # Panics
    ///
    /// Panics if `field == "cause"` or if the serialization of `value` fails.
    #[must_use]
    pub fn with_extension<E, V>(mut self, extension: E, value: V) -> Self
    where
        E: Into<CowStr>,
        V: serde::Serialize,
    {
        let extension = extension.into();
        match extension.as_ref() {
            "type" | "status" | "details" | "cause" | "" => {
                panic!("Invalid extension received: {}", extension)
            }
            _ => self.inner.extensions.insert(extension, value),
        }

        self
    }
}

/// Getters
impl Problem {
    /// A URI reference ([RFC 3986]) that identifies the problem type.
    ///
    /// The specification encourages that, when dereferenced, it provide
    /// human-readable documentation for the problem type. When this
    /// member is not present, its value is assumed to be `about:blank`.
    ///
    /// [RFC 3986]: https://tools.ietf.org/html/rfc3986
    pub const fn type_(&self) -> &Uri {
        &self.inner.r#type
    }

    /// A short, human-readable summary of the problem type.
    ///
    /// It SHOULD NOT change from occurrence to occurrence of the problem.
    pub fn title(&self) -> &str {
        &self.inner.title
    }

    /// The HTTP status code generated by the origin server for this
    /// occurrence of the problem.
    pub const fn status(&self) -> StatusCode {
        self.inner.status
    }

    /// A human-readable explanation specific to this occurrence of the
    /// problem.
    pub fn details(&self) -> &str {
        &self.inner.details
    }

    /// Extra members of the problem containing additional information
    /// about the specific occurrence.
    pub const fn extensions(&self) -> &Extensions {
        &self.inner.extensions
    }

    /// Extra members of the problem containing additional information
    /// about the specific occurrence.
    pub fn extensions_mut(&mut self) -> &mut Extensions {
        &mut self.inner.extensions
    }

    /// The internal cause of this problem.
    pub fn cause(&self) -> &(dyn std::error::Error + 'static) {
        &*self.inner.cause
    }
}

/// Error handling methods.
impl Problem {
    /// Get the [`Report`] of this instance.
    #[must_use]
    pub fn report(&self) -> &Report {
        self.inner.report()
    }

    /// Get the backtrace for this Error.
    pub fn backtrace(&self) -> Backtrace {
        (*self.inner.report().backtrace()).clone()
    }

    /// Location where this instance was created.
    pub fn location(&self) -> &'static Location<'static> {
        self.inner.report().location()
    }

    /// Returns true if `E` is the type of the cause of this problem.
    ///
    /// Useful to a failed result is caused by a specific error type.
    pub fn is<E>(&self) -> bool
    where
        E: std::error::Error + Send + Sync + 'static,
    {
        self.inner.cause.is::<E>()
    }

    /// Attempts to downcast the problem to a concrete type.
    ///
    /// # Errors
    ///
    /// Returns the original problem if the underlying cause is not of the
    /// specified type.
    pub fn downcast<E>(mut self) -> Result<E, Self>
    where
        E: std::error::Error + Send + Sync + 'static,
    {
        match self.inner.cause.downcast() {
            Ok(err) => Ok(err),
            Err(cause) => {
                self.inner.cause = cause;
                Err(self)
            }
        }
    }

    /// Attempt to downcast the problem to a concrete type by reference.
    pub fn downcast_ref<E>(&self) -> Option<&E>
    where
        E: std::error::Error + Send + Sync + 'static,
    {
        self.inner.cause.downcast_ref()
    }

    /// Attempts to isolate a specific cause to the `Err` variant.
    ///
    /// This is different from a downcast as we don't lose backtrace/source
    /// location information.
    ///
    /// This method is useful when the user wants to handle specific errors
    /// with `?`.
    ///
    /// # Errors
    ///
    /// Returns `Err` when `self` is an `E`.
    pub fn isolate<E>(self) -> Result<Self, Self>
    where
        E: std::error::Error + Send + Sync + 'static,
    {
        if self.is::<E>() {
            Err(self)
        } else {
            Ok(self)
        }
    }
}

/// Set of extensions of a [`Problem`].
#[derive(Debug, Clone, Default, serde::Serialize)]
#[serde(transparent)]
pub struct Extensions {
    inner: HashMap<CowStr, serde_json::Value>,
}

impl Extensions {
    /// Add an extension into the set.
    ///
    /// # Panics
    ///
    /// Panics if the serialization of `V` fails.
    pub fn insert<K, V>(&mut self, key: K, value: V)
    where
        K: Into<CowStr>,
        V: serde::Serialize,
    {
        self.inner.insert(key.into(), serde_json::json!(value));
    }

    /// Number of extensions.
    pub fn len(&self) -> usize {
        self.inner.len()
    }

    /// If we have no extensions.
    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }
}

impl<'e> IntoIterator for &'e Extensions {
    type IntoIter = ExtensionsIter<'e>;
    type Item = (&'e str, &'e serde_json::Value);

    fn into_iter(self) -> Self::IntoIter {
        ExtensionsIter(self.inner.iter().map(|(k, v)| (&**k, v)))
    }
}

use std::{collections::hash_map::Iter, iter::Map};

#[doc(hidden)]
#[allow(clippy::type_complexity)]
pub struct ExtensionsIter<'e>(
    Map<
        Iter<'e, Cow<'e, str>, serde_json::Value>,
        for<'a> fn((&'a Cow<'a, str>, &'a serde_json::Value)) -> (&'a str, &'a serde_json::Value),
    >,
);

impl<'e> Iterator for ExtensionsIter<'e> {
    type Item = (&'e str, &'e serde_json::Value);

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next()
    }
}

#[cfg(test)]
mod tests {
    use std::error::Error;

    use serde_json::json;

    use super::*;

    #[test]
    fn test_extensions() {
        let mut ext = Extensions::default();

        assert!(ext.is_empty());
        assert_eq!(ext.len(), 0);
        assert!(ext.into_iter().next().is_none());

        ext.insert("bla", "bla");

        assert_eq!(ext.len(), 1);
        assert!(!ext.is_empty());
        assert_eq!(ext.into_iter().next(), Some(("bla", &json!("bla"))));

        assert_eq!(json!(ext), json!({ "bla": "bla" }));
    }

    #[test]
    fn test_problem_with_extensions_good() {
        let mut error = http::failed_precondition();

        for (key, value) in [
            ("bla", json!("bla")),
            ("foo", json!(1)),
            ("bar", json!(1.2)),
            ("baz", json!([1.2])),
        ] {
            error = error.with_extension(key, value);
        }

        assert_eq!(error.extensions().len(), 4);
    }

    macro_rules! test_invalid_extension {
        ($test_fn: ident, $ext: literal) => {
            #[test]
            #[should_panic = concat!("Invalid extension received: ", $ext)]
            fn $test_fn() {
                let _res = http::failed_precondition().with_extension($ext, json!(1));
            }
        };
    }

    test_invalid_extension!(test_problem_with_extension_type, "type");
    test_invalid_extension!(test_problem_with_extension_status, "status");
    test_invalid_extension!(test_problem_with_extension_details, "details");
    test_invalid_extension!(test_problem_with_extension_cause, "cause");
    test_invalid_extension!(test_problem_with_extension_empty, "");

    #[test]
    fn test_problem_getter_type_() {
        assert_eq!(http::failed_precondition().type_(), "about:blank");
    }

    #[test]
    fn test_problem_getter_report() {
        let err = http::failed_precondition();
        let report = err.report();

        assert_eq!(err.location(), report.location());
    }

    #[test]
    fn test_problem_error_handling() {
        let err = http::failed_precondition();

        assert!(err.is::<http::PreconditionFailed>());
        assert!(err.downcast_ref::<http::PreconditionFailed>().is_some());
        assert!(err.isolate::<http::PreconditionFailed>().is_err());

        let err = http::failed_precondition();
        assert!(!err.is::<http::NotFound>());
        assert!(err.downcast_ref::<http::NotFound>().is_none());
        assert!(err.isolate::<http::NotFound>().is_ok());

        let err = http::failed_precondition();
        assert!(err.downcast::<http::PreconditionFailed>().is_ok());

        let err = http::failed_precondition();
        assert!(err.downcast::<http::NotFound>().is_err());
    }

    #[test]
    fn test_problem_source() {
        let err = http::failed_precondition();
        let source = err.source().unwrap() as *const dyn Error as *const ();
        let cause = err.cause() as *const dyn Error as *const ();

        assert!(core::ptr::eq(source, cause));
    }

    #[test]
    fn test_problem_serialize_no_type() {
        let err = http::failed_precondition()
            .with_detail("Failed a precondition")
            .with_extension("foo", "bar");

        assert_eq!(
            json!(err),
            json!({
                "detail": "Failed a precondition",
                "foo": "bar",
                "status": 412,
                "title": "Precondition Failed",
            })
        );
    }

    #[test]
    fn test_problem_serialize_type() {
        let err = Problem::custom(
            StatusCode::PRECONDITION_FAILED,
            Uri::from_static("https://my.beautiful.error"),
        )
        .with_detail("Failed a precondition")
        .with_extension("foo", "bar");

        assert_eq!(
            json!(err),
            json!({
                "detail": "Failed a precondition",
                "foo": "bar",
                "status": 412,
                "title": "Precondition Failed",
                "type": "https://my.beautiful.error/",
            })
        );
    }
}