Skip to main content

shdp/protocol/
errors.rs

1use std::fmt;
2
3///
4/// All the error types that can be returned.
5///
6/// # Example
7/// ```rust
8/// use shdp::prelude::common::error::ErrorKind;
9///
10/// let error = ErrorKind::NotFound;
11/// println!("{}", error);
12/// ```
13#[derive(Debug)]
14#[allow(dead_code)]
15pub enum ErrorKind {
16    /// The request could not be understood by the server due to malformed syntax.
17    BadRequest,
18    /// The request requires user authentication.
19    Unauthorized,
20    /// The payment is required.
21    PaymentRequired,
22    /// The server understood the request, but it refuses to authorize it.
23    Forbidden,
24    /// The server has not found anything matching the request.
25    NotFound,
26    /// The method specified in the request is not allowed for the resource identified by the request.
27    MethodNotAllowed,
28    /// The server cannot generate a response that the client will accept.
29    RequestTimeout,
30    /// The request could not be completed due to a conflict with the current state of the resource.
31    Conflict,
32    /// The requested resource is no longer available at the server and no forwarding address is known.
33    Gone,
34    /// The request entity is larger than the server is willing or able to process.
35    RequestEntityTooLarge,
36    /// The request range is not satisfiable.
37    RequestedRangeUnsatisfiable,
38    /// The server cannot or will not process the request due to an apparent client error.
39    ExpectationFailed,
40    /// The request has expired.
41    Expired,
42    /// The resource is locked.
43    Locked,
44    /// The server has not found anything matching the request.
45    NoResponse,
46    /// The request has been canceled.
47    Canceled,
48    /// The server encountered an unexpected condition that prevented it from fulfilling the request.
49    InternalServerError,
50    /// The server does not support the functionality required to fulfill the request.
51    NotImplemented,
52    /// The server is currently unable to handle the request due to a temporary overloading or maintenance of the server.
53    ServiceUnavailable,
54    /// The request is larger than the server is willing or able to process.
55    SizeConstraintViolation,
56    /// The server encountered an unexpected condition that prevented it from fulfilling the request.
57    ProtocolError,
58    /// The server does not support the HTTP protocol version used in the request.
59    UnknownVersion,
60    ///
61    /// User defined error. It can be used to wrap any error type.
62    ///
63    /// # Example
64    /// ```rust
65    /// use shdp::prelude::common::error::ErrorKind;
66    ///
67    /// fn main() {
68    ///     let error = ErrorKind::UserDefined(Box::new(std::io::Error::new(std::io::ErrorKind::Other, "User defined error")));
69    ///     println!("{}", error);
70    /// }
71    /// ```
72    UserDefined(Box<dyn std::error::Error>),
73}
74
75impl fmt::Display for ErrorKind {
76    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
77        match self {
78            ErrorKind::BadRequest => write!(f, "BadRequest"),
79            ErrorKind::Unauthorized => write!(f, "Unauthorized"),
80            ErrorKind::PaymentRequired => write!(f, "PaymentRequired"),
81            ErrorKind::Forbidden => write!(f, "Forbidden"),
82            ErrorKind::NotFound => write!(f, "NotFound"),
83            ErrorKind::MethodNotAllowed => write!(f, "MethodNotAllowed"),
84            ErrorKind::RequestTimeout => write!(f, "RequestTimeout"),
85            ErrorKind::Conflict => write!(f, "Conflict"),
86            ErrorKind::Gone => write!(f, "Gone"),
87            ErrorKind::RequestEntityTooLarge => write!(f, "RequestEntityTooLarge"),
88            ErrorKind::RequestedRangeUnsatisfiable => write!(f, "RequestedRangeUnsatisfiable"),
89            ErrorKind::ExpectationFailed => write!(f, "ExpectationFailed"),
90            ErrorKind::Expired => write!(f, "Expired"),
91            ErrorKind::Locked => write!(f, "Locked"),
92            ErrorKind::NoResponse => write!(f, "NoResponse"),
93            ErrorKind::Canceled => write!(f, "Canceled"),
94            ErrorKind::InternalServerError => write!(f, "InternalServerError"),
95            ErrorKind::NotImplemented => write!(f, "NotImplemented"),
96            ErrorKind::ServiceUnavailable => write!(f, "ServiceUnavailable"),
97            ErrorKind::SizeConstraintViolation => write!(f, "SizeConstraintViolation"),
98            ErrorKind::ProtocolError => write!(f, "ProtocolError"),
99            ErrorKind::UnknownVersion => write!(f, "UnknownVersion"),
100            ErrorKind::UserDefined(e) => write!(f, "{}", e),
101        }
102    }
103}
104
105///
106/// A basic error structure.
107/// # Example
108/// ```rust
109/// use shdp::prelude::common::error::{Error, ErrorKind};
110///
111/// let error = Error {
112///     code: 404,
113///     message: "Not Found".to_string(),
114///     kind: ErrorKind::NotFound,
115/// };
116///
117/// println!("{}", error);
118/// ```
119#[derive(Debug)]
120pub struct Error {
121    /// The error code.
122    pub code: u32,
123    /// The error message.
124    pub message: String,
125    /// The error kind.
126    pub kind: ErrorKind,
127}
128
129impl fmt::Display for Error {
130    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
131        write!(
132            f,
133            "Error: [{}]:{} -> {}",
134            self.kind, self.code, self.message
135        )
136    }
137}