1use thiserror::Error;
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub enum ClientCode {
19 BadRequest,
21 AuthFailed,
23 RateLimited,
25 PayloadTooLarge,
27 NotAllowed,
29 ResourceExhausted,
31 UnsupportedVersion,
33 Internal,
35}
36
37impl ClientCode {
38 pub fn as_str(self) -> &'static str {
40 match self {
41 ClientCode::BadRequest => "BAD_REQUEST",
42 ClientCode::AuthFailed => "AUTH_FAILED",
43 ClientCode::RateLimited => "RATE_LIMITED",
44 ClientCode::PayloadTooLarge => "PAYLOAD_TOO_LARGE",
45 ClientCode::NotAllowed => "NOT_ALLOWED",
46 ClientCode::ResourceExhausted => "RESOURCE_EXHAUSTED",
47 ClientCode::UnsupportedVersion => "UNSUPPORTED_VERSION",
48 ClientCode::Internal => "INTERNAL",
49 }
50 }
51}
52
53pub type Result<T> = std::result::Result<T, WsPrismError>;
55
56#[derive(Debug, Error)]
58pub enum WsPrismError {
59 #[error("bad request: {0}")]
60 BadRequest(String),
61 #[error("auth failed")]
62 AuthFailed,
63 #[error("rate limited")]
64 RateLimited,
65 #[error("payload too large")]
66 PayloadTooLarge,
67 #[error("not allowed: {0}")]
68 NotAllowed(String),
69 #[error("resource exhausted: {0}")]
70 ResourceExhausted(String),
71 #[error("unsupported protocol version")]
72 UnsupportedVersion,
73 #[error("internal: {0}")]
74 Internal(String),
75}
76
77impl WsPrismError {
78 pub fn client_code(&self) -> ClientCode {
80 match self {
81 WsPrismError::BadRequest(_) => ClientCode::BadRequest,
82 WsPrismError::AuthFailed => ClientCode::AuthFailed,
83 WsPrismError::RateLimited => ClientCode::RateLimited,
84 WsPrismError::PayloadTooLarge => ClientCode::PayloadTooLarge,
85 WsPrismError::NotAllowed(_) => ClientCode::NotAllowed,
86 WsPrismError::ResourceExhausted(_) => ClientCode::ResourceExhausted,
87 WsPrismError::UnsupportedVersion => ClientCode::UnsupportedVersion,
88 WsPrismError::Internal(_) => ClientCode::Internal,
89 }
90 }
91}