Skip to main content

spider_agent/automation/
engine_error.rs

1//! Engine error types for automation.
2
3use std::{error::Error as StdError, fmt};
4
5/// Convenience result type used throughout the remote multimodal engine.
6pub type EngineResult<T> = Result<T, EngineError>;
7
8/// Errors produced by the remote multimodal engine.
9///
10/// This error type is intentionally lightweight and is suitable
11/// for surfacing from public APIs.
12///
13/// It covers:
14/// - transport failures when calling the remote endpoint,
15/// - JSON serialization/deserialization failures,
16/// - schema mismatches in OpenAI-compatible responses,
17/// - non-success responses returned by the remote provider,
18/// - unsupported operations due to compile-time feature flags.
19#[derive(Debug)]
20pub enum EngineError {
21    /// HTTP-layer failure (request could not be sent, connection error, timeout, etc.).
22    Http(reqwest::Error),
23    /// JSON serialization/deserialization failure when building or parsing payloads.
24    Json(serde_json::Error),
25    /// A required field was missing in a parsed JSON payload.
26    ///
27    /// Example: missing `"choices[0].message.content"` in an OpenAI-compatible response.
28    MissingField(&'static str),
29    /// A field was present but had an unexpected type or shape.
30    ///
31    /// Example: `"steps"` exists but is not an array.
32    InvalidField(&'static str),
33    /// The remote endpoint returned a non-success status or a server-side error.
34    ///
35    /// The contained string should be a human-readable explanation suitable for logs.
36    Remote(String),
37    /// The operation is not supported in the current build configuration.
38    ///
39    /// Example: calling browser automation without the `chrome` feature.
40    Unsupported(&'static str),
41}
42
43impl fmt::Display for EngineError {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        match self {
46            EngineError::Http(e) => write!(f, "http error: {e}"),
47            EngineError::Json(e) => write!(f, "json error: {e}"),
48            EngineError::MissingField(s) => write!(f, "missing field: {s}"),
49            EngineError::InvalidField(s) => write!(f, "invalid field: {s}"),
50            EngineError::Remote(s) => write!(f, "remote error: {s}"),
51            EngineError::Unsupported(s) => write!(f, "unsupported: {s}"),
52        }
53    }
54}
55
56impl StdError for EngineError {}
57
58impl From<reqwest::Error> for EngineError {
59    fn from(e: reqwest::Error) -> Self {
60        EngineError::Http(e)
61    }
62}
63
64impl From<serde_json::Error> for EngineError {
65    fn from(e: serde_json::Error) -> Self {
66        EngineError::Json(e)
67    }
68}