Skip to main content

endpoint_libs/libs/
error_code.rs

1use serde::*;
2
3/// `ErrorCode` is a wrapper around `u32` that represents an error code.
4#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
5pub struct ErrorCode {
6    /// The error code (e.g. `100400` for BadRequest).
7    code: u32,
8}
9
10impl ErrorCode {
11    /// Indicated a bad request.
12    pub const BAD_REQUEST: Self = Self::new(100400);
13
14    /// Indicates forbidden access.
15    pub const FORBIDDEN: Self = Self::new(100403);
16
17    /// Indicates an internal server error.
18    pub const INTERNAL_ERROR: Self = Self::new(100500);
19
20    /// Indicates a non-implemented endpoint.
21    pub const NOT_IMPLEMENTED: Self = Self::new(100501);
22
23    /// Create a new `ErrorCode` from a `u32`.
24    pub const fn new(code: u32) -> Self {
25        Self { code }
26    }
27
28    /// Get the `ErrorCode` as a `u32`.
29    /// Just returns the code field.
30    pub const fn to_u32(self) -> u32 {
31        self.code
32    }
33
34    /// Getter for the `ErrorCode` code field.
35    pub const fn code(self) -> u32 {
36        self.to_u32()
37    }
38}