Skip to main content

inferd_proto/
error.rs

1//! Error types for the wire-format crate.
2
3use serde::{Deserialize, Serialize};
4
5/// Machine-readable error classification carried on `error` response frames.
6///
7/// See `docs/protocol-v1.md` ยง"Response stream" and ADR 0008 for the rationale
8/// behind exposing this distinct from the human-readable `message` field.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
10#[serde(rename_all = "snake_case")]
11pub enum ErrorCode {
12    /// Admission queue full at submit time. Caller may retry immediately or with backoff.
13    QueueFull,
14    /// Selected backend errored before or during generation. Caller may retry.
15    BackendUnavailable,
16    /// Request failed validation. Caller should not retry without changing the request.
17    InvalidRequest,
18    /// Frame exceeded the 64 MiB cap. Connection is closed.
19    FrameTooLarge,
20    /// Daemon-side bug or unexpected condition.
21    Internal,
22}
23
24/// Errors produced by the proto crate while parsing or validating frames.
25#[derive(Debug, thiserror::Error)]
26pub enum ProtoError {
27    /// A single NDJSON frame exceeded `MAX_FRAME_BYTES`.
28    #[error("frame exceeds {} byte cap", crate::MAX_FRAME_BYTES)]
29    FrameTooLarge,
30
31    /// I/O error reading the underlying transport.
32    #[error("io error: {0}")]
33    Io(#[from] std::io::Error),
34
35    /// Frame bytes were not valid JSON.
36    #[error("decode: {0}")]
37    Decode(#[from] serde_json::Error),
38
39    /// Frame parsed but failed semantic validation.
40    #[error("invalid request: {0}")]
41    InvalidRequest(String),
42}
43
44impl ProtoError {
45    /// Map a parse/validate error to the wire-level `ErrorCode` a daemon should
46    /// emit on the response stream.
47    pub fn to_error_code(&self) -> ErrorCode {
48        match self {
49            ProtoError::FrameTooLarge => ErrorCode::FrameTooLarge,
50            ProtoError::Decode(_) | ProtoError::InvalidRequest(_) => ErrorCode::InvalidRequest,
51            ProtoError::Io(_) => ErrorCode::Internal,
52        }
53    }
54}