Skip to main content

ferrin_spec/error/
config.rs

1//! Argument, prompt and configuration errors.
2
3use super::BoxError;
4use crate::json::JsonValue;
5
6/// A call argument is invalid.
7#[derive(Debug, thiserror::Error)]
8#[error("invalid argument `{argument}`: {message}")]
9pub struct InvalidArgumentError {
10    /// Name of the argument.
11    pub argument: String,
12    /// What is wrong with it.
13    pub message: String,
14    /// Underlying cause.
15    #[source]
16    pub cause: Option<BoxError>,
17}
18
19impl InvalidArgumentError {
20    /// Creates an error for `argument`.
21    #[must_use]
22    pub fn new(argument: impl Into<String>, message: impl Into<String>) -> Self {
23        Self {
24            argument: argument.into(),
25            message: message.into(),
26            cause: None,
27        }
28    }
29
30    /// Sets the underlying cause.
31    #[must_use]
32    pub fn with_cause(mut self, cause: impl std::error::Error + Send + Sync + 'static) -> Self {
33        self.cause = Some(Box::new(cause));
34        self
35    }
36}
37
38/// The prompt cannot be used with this provider.
39#[derive(Debug, thiserror::Error)]
40#[error("invalid prompt: {message}")]
41pub struct InvalidPromptError {
42    /// What is wrong with the prompt.
43    pub message: String,
44    /// The offending prompt (or part of it) as JSON, if available.
45    pub prompt: Option<JsonValue>,
46    /// Underlying cause.
47    #[source]
48    pub cause: Option<BoxError>,
49}
50
51impl InvalidPromptError {
52    /// Creates an error with a message only.
53    #[must_use]
54    pub fn new(message: impl Into<String>) -> Self {
55        Self {
56            message: message.into(),
57            prompt: None,
58            cause: None,
59        }
60    }
61
62    /// Attaches the offending prompt.
63    #[must_use]
64    pub fn with_prompt(mut self, prompt: JsonValue) -> Self {
65        self.prompt = Some(prompt);
66        self
67    }
68
69    /// Sets the underlying cause.
70    #[must_use]
71    pub fn with_cause(mut self, cause: impl std::error::Error + Send + Sync + 'static) -> Self {
72        self.cause = Some(Box::new(cause));
73        self
74    }
75}
76
77/// The API key could not be loaded.
78#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
79#[error("{message}")]
80pub struct LoadApiKeyError {
81    /// Explanation (never contains the key).
82    pub message: String,
83}
84
85impl LoadApiKeyError {
86    /// Creates an error with `message`.
87    #[must_use]
88    pub fn new(message: impl Into<String>) -> Self {
89        Self {
90            message: message.into(),
91        }
92    }
93}
94
95/// A required setting could not be loaded.
96#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
97#[error("{message}")]
98pub struct LoadSettingError {
99    /// Explanation.
100    pub message: String,
101}
102
103impl LoadSettingError {
104    /// Creates an error with `message`.
105    #[must_use]
106    pub fn new(message: impl Into<String>) -> Self {
107        Self {
108            message: message.into(),
109        }
110    }
111}