1use crate::message::{ErrorResponse, VerdictResponse};
4use reqwest::StatusCode;
5use std::sync::PoisonError;
6use thiserror::Error;
7use tokio::sync::broadcast::error::SendError;
8use tokio::sync::oneshot::error::RecvError;
9use tokio::time::error::Elapsed;
10use websockets::WebSocketError;
11
12pub type VResult<T> = Result<T, Error>;
14
15#[non_exhaustive]
17#[derive(Error, Debug, Clone)]
18pub enum Error {
19 #[error("WebSocket Error: `{0}`")]
21 WebSocket(String),
22 #[error("Serialization Error: `{0}`")]
24 DeSerialization(String),
25 #[error("Cannot acquire message lock: `{0}`")]
27 Lock(String),
28 #[error("Received an invalid verdict type: `{0}`")]
30 InvalidVerdict(String),
31 #[error("Request was cancelled")]
33 Cancelled,
34 #[error("Invalid frame received")]
36 InvalidFrame,
37 #[error("Invalid message received: `{0}`")]
39 InvalidMessage(String),
40 #[error("No connection established. Did you forget to connect?")]
42 NoConnection,
43 #[error("Upload URL not set but expected")]
45 NoUploadUrl,
46 #[error("IO Error: `{0}`")]
48 IoError(String),
49 #[error("Invalid SHA256: `{0}`")]
51 InvalidSha256(String),
52 #[error("Failed to send file: `{0}`")]
54 FailedRequest(String),
55 #[error("Server answered with status code: `{0}` `{1}`")]
57 FailedUploadFile(StatusCode, String),
58 #[error("Missing authentication token for file upload")]
60 MissingAuthToken,
61 #[error("Unauthorized: `{0}`")]
63 Unauthorized(String),
64 #[error("The result channel failed: `{0}`")]
66 ResultChannelError(String),
67 #[error("Error response from the server")]
69 ErrorResponse(ErrorResponse),
70 #[error("Failed to get authentication token. Status code `{0}` with message `{1}`")]
72 FailedAuthTokenRequest(StatusCode, String),
73 #[error("No session id in authentication response set")]
76 NoSessionIdInAuthResp,
77 #[error("Connection was closed")]
79 ConnectionClosed,
80}
81
82impl From<PoisonError<std::sync::MutexGuard<'_, websockets::WebSocketWriteHalf>>> for Error {
83 fn from(e: PoisonError<std::sync::MutexGuard<'_, websockets::WebSocketWriteHalf>>) -> Self {
84 Self::Lock(e.to_string())
85 }
86}
87
88impl From<WebSocketError> for Error {
89 fn from(e: WebSocketError) -> Self {
90 Self::WebSocket(e.to_string())
91 }
92}
93
94impl From<serde_json::Error> for Error {
95 fn from(e: serde_json::Error) -> Self {
96 Self::DeSerialization(e.to_string())
97 }
98}
99
100impl From<std::io::Error> for Error {
101 fn from(e: std::io::Error) -> Self {
102 Self::IoError(e.to_string())
103 }
104}
105
106impl From<reqwest::Error> for Error {
107 fn from(e: reqwest::Error) -> Self {
108 Self::FailedRequest(e.to_string())
109 }
110}
111
112impl From<tokio::sync::broadcast::error::SendError<Result<VerdictResponse, Error>>> for Error {
113 fn from(e: SendError<Result<VerdictResponse, Error>>) -> Self {
114 Self::ResultChannelError(e.to_string())
115 }
116}
117
118impl From<RecvError> for Error {
119 fn from(e: RecvError) -> Self {
120 Self::ResultChannelError(e.to_string())
121 }
122}
123
124impl From<Elapsed> for Error {
125 fn from(_: Elapsed) -> Self {
126 Self::Cancelled
127 }
128}