cqrs_es2/errors/user_error.rs
1use std::{
2 collections::HashMap,
3 fmt::{
4 Debug,
5 Display,
6 Formatter,
7 Result as fmtResult,
8 },
9};
10
11use serde::{
12 Deserialize,
13 Serialize,
14};
15
16/// Payload for an `Error::UserError`, somewhat modeled on
17/// the errors produced by the [`validator`](https://github.com/Keats/validator) package. This payload implements `Serialize`
18/// with the intention of allowing the user to return this object as
19/// the response payload.
20#[derive(Debug, PartialEq, Serialize, Deserialize)]
21pub struct UserError {
22 /// An optional code to indicate the a user-defined error.
23 pub code: Option<String>,
24
25 /// An optional message describing the error, meant to be
26 /// returned to the user.
27 pub message: Option<String>,
28
29 /// Optional additional parameters for adding additional context
30 /// to the error.
31 pub params: Option<HashMap<String, String>>,
32}
33
34impl Display for UserError {
35 fn fmt(
36 &self,
37 f: &mut Formatter<'_>,
38 ) -> fmtResult {
39 write!(
40 f,
41 "UserError - code: {:?}\n message: {:?}\n params: {:?}",
42 &self.code, &self.message, &self.params
43 )
44 }
45}