svc_error/
lib.rs

1#![deny(missing_debug_implementations, missing_docs)]
2
3//! SvcError is an implementation of RFC7807: Problem Details for HTTP APIs.
4//!
5//! It's possible to attach some metadata to the error (only `payload` now).
6
7use http::StatusCode;
8use std::collections::HashMap;
9
10/// Problem Details
11pub trait ProblemDetails: ProblemDetailsReadOnly + ProblemDetailsMut + serde::Serialize {}
12
13/// Getters for Problem Details and accompanying metadata
14
15impl ProblemDetails for Error {}
16
17/// Getters for Problem Details
18pub trait ProblemDetailsReadOnly {
19    /// Return a URI reference RFC3986 that identifies the
20    /// problem type.  This specification encourages that, when
21    /// dereferenced, it provide human-readable documentation for the
22    /// problem type. When this member is not present, its value is assumed
23    /// to be "about:blank".
24    fn kind(&self) -> &str;
25
26    /// Return a short, human-readable summary of the problem
27    /// type. It SHOULD NOT change from occurrence to occurrence of the
28    /// problem, except for purposes of localization
29    fn title(&self) -> &str;
30
31    /// Return the HTTP status code generated by the origin server for this
32    /// occurrence of the problem.
33    fn status_code(&self) -> StatusCode;
34
35    /// Return a human-readable explanation specific to this occurrence of the problem.
36    fn detail(&self) -> Option<&str>;
37
38    /// Return all error related extra values.
39    fn extras(&self) -> &HashMap<String, String>;
40}
41
42impl ProblemDetailsReadOnly for Error {
43    fn status_code(&self) -> StatusCode {
44        self.status_code()
45    }
46
47    fn kind(&self) -> &str {
48        self.kind()
49    }
50
51    fn title(&self) -> &str {
52        self.title()
53    }
54
55    fn detail(&self) -> Option<&str> {
56        self.detail()
57    }
58
59    fn extras(&self) -> &HashMap<String, String> {
60        self.extras()
61    }
62}
63
64/// Mutation methods for Problem Details.
65/// This methods are in separate trait to make ProblemDetailsReadOnly object safe.
66pub trait ProblemDetailsMut {
67    /// Set a kind and a title.
68    fn set_kind(&mut self, kind: &str, title: &str) -> &mut Self;
69
70    /// Set a status code.
71    fn set_status_code(&mut self, value: StatusCode) -> &mut Self;
72
73    /// Set a detail.
74    fn set_detail(&mut self, detail: &str) -> &mut Self;
75}
76
77impl ProblemDetailsMut for Error {
78    fn set_kind(&mut self, kind: &str, title: &str) -> &mut Self {
79        self.set_kind(kind, title)
80    }
81
82    fn set_status_code(&mut self, value: StatusCode) -> &mut Self {
83        self.set_status_code(value)
84    }
85
86    fn set_detail(&mut self, detail: &str) -> &mut Self {
87        self.set_detail(detail)
88    }
89}
90
91pub use self::error::{Builder, Error};
92mod error;
93
94/// Extensions
95pub mod extension;