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
pub use http::{StatusCode, Uri};
use super::{CowStr, Extensions, Problem};
/// Define a new custom problem type.
///
/// Although defining a new custom type requires only implementing
/// the [`CustomProblem`] trait, this macro simplifies the code,
/// removing boilerplate from the definition.
///
/// # Example
///
/// ```
/// use http_problem::prelude::{StatusCode, Uri};
/// problem::define_custom_type! {
/// /// An error that occurs when a transaction cannot be done
/// /// because one of the accounts doesn't have enough credit.
/// type OutOfCredit {
/// type: "https://example.com/probs/out-of-credit",
/// title: "You do not have enough credit",
/// status: StatusCode::FORBIDDEN,
/// detail(p): format!("You current balance is {}, but that costs {}", p.balance, p.cost),
/// extensions: {
/// balance: i64,
/// cost: i64,
/// accounts: Vec<String>
/// }
/// }
/// }
///
/// fn do_transaction() -> problem::Result<()> {
/// Err(OutOfCredit {
/// balance: 50,
/// cost: 30,
/// accounts: vec!["/account/12345".into(), "/account/67890".into()]
/// }.into())
/// }
///
/// fn main() {
/// let problem = do_transaction().unwrap_err();
/// assert_eq!(problem.type_(), &Uri::from_static("https://example.com/probs/out-of-credit"));
/// assert_eq!(problem.title(), "You do not have enough credit");
/// assert_eq!(problem.status(), StatusCode::FORBIDDEN);
/// assert_eq!(problem.details(), "You current balance is 50, but that costs 30");
/// assert_eq!(problem.extensions().len(), 3);
/// }
/// ```
#[macro_export]
macro_rules! define_custom_type {
($(#[$meta: meta])* type $rstyp: ident {
type: $typ:literal,
title: $title:literal,
status: $status: expr,
detail($prob: ident): $detail: expr,
extensions: {
$($field:ident: $field_ty: ty),* $(,)?
} $(,)?
}) => {
$(#[$meta])*
#[derive(Debug)]
pub struct $rstyp {
$(pub $field: $field_ty),*
}
impl ::std::fmt::Display for $rstyp {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
writeln!(f, "{}", <Self as $crate::prelude::CustomProblem>::details(self))
}
}
impl ::std::error::Error for $rstyp {}
impl $crate::prelude::CustomProblem for $rstyp {
fn problem_type(&self) -> $crate::prelude::Uri {
$crate::prelude::Uri::from_static($typ)
}
fn title(&self) -> &'static str {
$title
}
fn status_code(&self) -> $crate::prelude::StatusCode {
$status
}
fn details(&self) -> $crate::CowStr {
let $prob = self;
$detail.into()
}
fn add_extensions(
&self,
_extensions: &mut $crate::Extensions)
{
$(
_extensions.insert(stringify!($field), &self.$field);
)*
}
}
};
}
/// A trait defining custom problem types.
///
/// Implementing this trait provides enough information to create a
/// [`Problem`] instance with the correct values for each field.
///
/// There is no need to implement `From<Self> for Problem` if you
/// implement this trait.
///
/// See [`define_custom_type!`] for a convenient way of implementing
/// this trait.
pub trait CustomProblem: std::error::Error + Send + Sync + 'static {
/// A URI reference that identifies the problem type.
///
/// See [`Problem::type_`] more information.
fn problem_type(&self) -> Uri;
/// A short, human-readable summary of the problem type.
///
/// See [`Problem::title`] for more information.
fn title(&self) -> &'static str;
/// The HTTP status code for this problem type.
///
/// See [`Problem::status`] for more information.
fn status_code(&self) -> StatusCode;
/// A human-readable explanation of the occurrence.
///
/// See [`Problem::details`] for more information.
fn details(&self) -> CowStr;
/// Add extensions to the final problem instance.
///
/// See [`Problem::with_extension`] for more info.
fn add_extensions(&self, extensions: &mut Extensions);
}
impl<C: CustomProblem> From<C> for Problem {
#[track_caller]
fn from(custom: C) -> Self {
let mut problem = Self::custom(custom.status_code(), custom.problem_type())
.with_title(custom.title())
.with_detail(custom.details());
custom.add_extensions(problem.extensions_mut());
problem.with_cause(custom)
}
}
#[cfg(test)]
mod tests {
use super::*;
define_custom_type! {
/// An error that occurs when a transaction cannot be done
/// because one of the accounts doesn't have enough credit.
type OutOfCredit {
type: "https://example.com/probs/out-of-credit",
title: "You do not have enough credit",
status: StatusCode::FORBIDDEN,
detail(p): format!("You current balance is {}, but that costs {}", p.balance, p.cost),
extensions: {
balance: i64,
cost: i64,
accounts: Vec<String>
}
}
}
#[test]
fn test_macro_output() {
let error = OutOfCredit {
balance: 30,
cost: 50,
accounts: vec!["aaa".into(), "bbb".into()],
};
assert_eq!(error.title(), "You do not have enough credit");
assert_eq!(error.status_code(), StatusCode::FORBIDDEN);
assert_eq!(
error.details(),
"You current balance is 30, but that costs 50"
);
}
#[test]
fn test_custom_problem_to_problem() {
let error = OutOfCredit {
balance: 30,
cost: 50,
accounts: vec!["aaa".into(), "bbb".into()],
};
let prob: Problem = error.into();
assert_eq!(prob.title(), "You do not have enough credit");
assert_eq!(prob.status(), StatusCode::FORBIDDEN);
assert_eq!(
prob.details(),
"You current balance is 30, but that costs 50"
);
}
}