resp_result/resp_error/mod.rs
1use std::borrow::Cow;
2
3/// the error when [`RespResult`](crate::RespResult) is `Err(_)`
4pub trait RespError {
5 /// message for logger
6 fn log_message(&self) -> Cow<'_, str>;
7 /// message for response
8 ///
9 /// ## Default
10 /// the [`RespError::resp_message`] default is equal to [`RespError::log_message`]
11 #[inline]
12 fn resp_message(&self) -> Cow<'_, str> {
13 self.log_message()
14 }
15
16 /// the http code of this error
17 ///
18 /// ## Default
19 /// the default http code is `500 Internal Server Error`
20 #[inline]
21 fn http_code(&self) -> http::StatusCode {
22 http::StatusCode::INTERNAL_SERVER_ERROR
23 }
24
25 #[cfg(feature = "extra-error")]
26 /// the associate type of extra message
27 type ExtraMessage: serde::Serialize + 'static + Sized + std::fmt::Display;
28 #[cfg(feature = "extra-error")]
29 /// get the extra message
30 fn extra_message(&self) -> Self::ExtraMessage;
31
32 /// when `fix-field = true` using this value serialize error message
33 ///
34 /// ## Default
35 /// default is [`None`](Option::None), it will serialize to `null`
36 #[inline]
37 fn resp_message_default() -> Option<Cow<'static, str>> {
38 None
39 }
40
41 /// when `fix-field = true` using this value serialize extra error message
42 ///
43 /// ## Default
44 /// default is [`None`], it will be serialize to `null`
45 #[cfg(feature = "extra-error")]
46 #[inline]
47 fn extra_message_default() -> Option<Self::ExtraMessage> {
48 None
49 }
50}