actix_msgpack/
msgpack_error.rs

1use actix_web::{error::PayloadError, HttpResponse, ResponseError};
2use derive_more::Display;
3use rmp_serde::{decode::Error as RmpSerdeDecodeError, encode::Error as RmpSerdeEncodeError};
4
5#[derive(Debug, Display)]
6pub enum MsgPackError {
7	/// Payload size is bigger than limit
8	#[display(fmt = "Payload size is bigger than limit")]
9	Overflow,
10
11	/// Content type error
12	#[display(fmt = "Content type error")]
13	ContentType,
14
15	/// Deserialize error
16	#[display(fmt = "Deserialize error: {_0}")]
17	Deserialize(RmpSerdeDecodeError),
18
19	/// Serialize error
20	#[display(fmt = "Serialize error: {_0}")]
21	Serialize(RmpSerdeEncodeError),
22
23	/// Payload error
24	#[display(fmt = "Error that occur during reading payload: {_0}")]
25	Payload(PayloadError),
26}
27
28impl ResponseError for MsgPackError {
29	fn error_response(&self) -> HttpResponse {
30		match *self {
31			MsgPackError::Overflow => HttpResponse::PayloadTooLarge().into(),
32			_ => HttpResponse::BadRequest().into(),
33		}
34	}
35}
36
37impl From<PayloadError> for MsgPackError {
38	fn from(err: PayloadError) -> MsgPackError {
39		MsgPackError::Payload(err)
40	}
41}
42
43impl From<RmpSerdeDecodeError> for MsgPackError {
44	fn from(err: RmpSerdeDecodeError) -> MsgPackError {
45		MsgPackError::Deserialize(err)
46	}
47}