rusp_lib/usp_builder/
error.rs1use crate::usp::{Body, Error};
2
3use crate::usp_errors;
4
5use crate::usp::mod_Body::OneOfmsg_body::error;
6use crate::usp::mod_Error::ParamError;
7
8use anyhow::Result;
9
10#[derive(Clone)]
11pub struct ErrorBuilder {
12 code: u32,
13 message: Option<String>,
14 param_errs: Vec<(String, u32, String)>,
15}
16
17impl ErrorBuilder {
18 #[must_use]
19 pub const fn new() -> Self {
20 Self {
21 code: 0,
22 message: None,
23 param_errs: vec![],
24 }
25 }
26
27 #[must_use]
28 pub fn set_err(mut self, code: u32, message: Option<String>) -> Self {
29 self.code = code;
30 self.message = message;
31 self
32 }
33
34 #[must_use]
35 pub fn with_param_errs(mut self, errs: Vec<(String, u32, String)>) -> Self {
36 self.param_errs = errs;
37 self
38 }
39
40 pub fn build(self) -> Result<Body> {
41 let message = self
42 .message
43 .clone()
44 .unwrap_or_else(|| usp_errors::get_err_msg(self.code).to_string());
45
46 let param_errs = self.param_errs;
47
48 Ok(Body {
49 msg_body: error({
50 Error {
51 err_code: self.code,
52 err_msg: message,
53 param_errs: param_errs
54 .into_iter()
55 .map(|(param_path, err_code, err_msg)| ParamError {
56 param_path,
57 err_code,
58 err_msg,
59 })
60 .collect(),
61 }
62 }),
63 })
64 }
65}