mutant_client/
error.rs

1use mutant_protocol::Response;
2pub use thiserror::Error;
3use wasm_bindgen::JsValue;
4
5#[derive(Error, Debug)]
6pub enum ClientError {
7    #[error("WebSocket connection failed: {0}")]
8    ConnectionError(String),
9
10    #[error("WebSocket communication error: {0}")]
11    WebSocketError(String),
12
13    #[error("URL parsing error: {0}")]
14    UrlParseError(#[from] url::ParseError),
15
16    #[error("JSON serialization error: {0}")]
17    SerializationError(#[from] serde_json::Error),
18
19    #[error("JSON deserialization error: {0}")]
20    DeserializationError(serde_json::Error),
21
22    #[error("Received message is not valid text: {0}")]
23    NonTextMessageReceived(String),
24
25    #[error("Received unexpected response from server: {0:?}")]
26    UnexpectedResponse(Response),
27
28    #[error("Server returned an error: {0}")]
29    ServerError(String),
30
31    #[error("Task ID not found in response when expected")]
32    TaskIdMissing,
33
34    #[error("Client is not connected or connection closed")]
35    NotConnected,
36
37    #[error("Failed to interact with JavaScript API: {0}")]
38    JsError(String),
39
40    #[error("Internal client error: {0}")]
41    InternalError(String),
42
43    #[error("Task with ID {0} not found")]
44    TaskNotFound(mutant_protocol::TaskId),
45}
46
47// Helper to convert JsValue to ClientError
48impl From<JsValue> for ClientError {
49    fn from(value: JsValue) -> Self {
50        ClientError::JsError(format!("{:?}", value))
51    }
52}