Skip to main content

clankerdiff_protocol/shared/
error.rs

1use serde::{Deserialize, Serialize};
2use thiserror::Error;
3
4#[derive(Debug, Error)]
5pub enum ProtocolError {
6    #[error("update refers to a file the client does not hold, or repeats a path")]
7    Files,
8    #[error("message encoding: {0}")]
9    Encoding(#[from] serde_json::Error),
10    #[error("expected a text protocol message")]
11    MessageType,
12}
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
15pub enum RemoteErrorCode {
16    UnsupportedVersion,
17    Protocol,
18    Busy,
19    Git,
20    Watcher,
21    Cancelled,
22}
23
24#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Error)]
25#[error("{message}")]
26pub struct RemoteError {
27    pub code: RemoteErrorCode,
28    pub message: String,
29}
30
31impl From<ProtocolError> for RemoteError {
32    fn from(error: ProtocolError) -> Self {
33        Self::new(RemoteErrorCode::Protocol, error.to_string())
34    }
35}
36
37impl RemoteError {
38    #[must_use]
39    pub fn new(code: RemoteErrorCode, message: impl Into<String>) -> Self {
40        Self {
41            code,
42            message: message.into(),
43        }
44    }
45}