Skip to main content

razeri_plugin_api/
error.rs

1// Copyright (C) 2026 Gabriel Shea
2// Licensed under either of the Apache License, Version 2.0 or the MIT
3// license, at your option.
4
5//! Error types for Razeri plugin API.
6
7use thiserror::Error;
8
9/// Errors from storage provider operations.
10#[derive(Debug, Error)]
11#[non_exhaustive]
12pub enum StorageError {
13    /// Storage configuration is missing or invalid.
14    #[error("storage configuration error: {0}")]
15    ConfigError(String),
16    /// An I/O error occurred during a storage operation.
17    #[error("storage I/O error: {0}")]
18    IoError(String),
19    /// The requested storage provider is not registered.
20    #[error("storage provider not found: {0}")]
21    ProviderNotFound(String),
22}
23
24/// Errors from LLM client operations.
25#[derive(Debug, Error)]
26#[non_exhaustive]
27pub enum LlmError {
28    /// An HTTP transport error occurred.
29    #[error("HTTP error: {0}")]
30    HttpError(String),
31    /// The response body could not be parsed as JSON.
32    #[error("JSON parse error: {0}")]
33    JsonError(String),
34    /// The provider returned a non-success HTTP status.
35    #[error("provider error (status {status}): {message}")]
36    ProviderError {
37        /// HTTP status code returned by the provider.
38        status: u16,
39        /// Error message from the provider.
40        message: String,
41    },
42    /// The requested LLM provider is not registered.
43    #[error("unsupported provider: {0}")]
44    UnsupportedProvider(String),
45    /// An API key is required but was not configured.
46    #[error("missing API key for provider: {0}")]
47    MissingApiKey(String),
48    /// The response format did not match the expected schema.
49    #[error("unexpected response format: {0}")]
50    UnexpectedFormat(String),
51}
52
53impl LlmError {
54    /// Returns `true` for errors that may succeed on retry (connection drops,
55    /// server overload, etc.).
56    #[must_use]
57    pub fn is_transient(&self) -> bool {
58        match self {
59            LlmError::HttpError(_) => true,
60            LlmError::ProviderError { status, .. } => matches!(status, 429 | 502 | 503 | 504),
61            _ => false,
62        }
63    }
64}
65
66/// Errors from event sink operations.
67#[derive(Debug, Error)]
68#[non_exhaustive]
69pub enum EventSinkError {
70    /// A generic event sink error.
71    #[error("event sink error: {0}")]
72    SinkError(String),
73    /// The event sink could not connect to its backend.
74    #[error("event sink connection failed: {0}")]
75    ConnectionFailed(String),
76}
77
78/// Errors from language renderer operations.
79#[derive(Debug, Error)]
80#[non_exhaustive]
81pub enum LanguageError {
82    /// A rendering error occurred for the target language.
83    #[error("language rendering error: {0}")]
84    RenderError(String),
85    /// The requested language feature is not supported by this renderer.
86    #[error("unsupported language feature: {0}")]
87    UnsupportedFeature(String),
88}