ray/
error.rs

1use std::path::PathBuf;
2use thiserror::Error;
3
4/// Errors returned by Ray requests.
5///
6/// ```rust
7/// use ray::RayError;
8///
9/// let err = RayError::InvalidInput {
10///     field: "url",
11///     message: "missing scheme".to_string(),
12/// };
13/// assert!(matches!(err, RayError::InvalidInput { .. }));
14/// ```
15#[derive(Debug, Error)]
16pub enum RayError {
17    #[error("failed to serialize payload")]
18    Serialize(#[from] serde_json::Error),
19
20    #[error("invalid JSON string: {source} (input: {input})")]
21    InvalidJson {
22        input: String,
23        #[source]
24        source: serde_json::Error,
25    },
26
27    #[error("failed to read file {path}: {source}")]
28    FileRead {
29        path: PathBuf,
30        #[source]
31        source: std::io::Error,
32    },
33
34    #[error("invalid input for {field}: {message}")]
35    InvalidInput {
36        field: &'static str,
37        message: String,
38    },
39
40    #[error("transport error: {source}")]
41    Transport {
42        #[source]
43        source: Box<dyn std::error::Error + Send + Sync>,
44    },
45
46    #[error("ray server returned status {status}: {body}")]
47    HttpStatus { status: u16, body: String },
48}
49
50impl RayError {
51    pub(crate) fn transport<E>(err: E) -> Self
52    where
53        E: std::error::Error + Send + Sync + 'static,
54    {
55        Self::Transport {
56            source: Box::new(err),
57        }
58    }
59
60    pub(crate) fn invalid_input(field: &'static str, message: impl Into<String>) -> Self {
61        Self::InvalidInput {
62            field,
63            message: message.into(),
64        }
65    }
66}