switchyard_translation/
error.rs1use thiserror::Error;
7
8use crate::format::FormatId;
9
10pub type Result<T> = std::result::Result<T, TranslationError>;
12
13#[derive(Debug, Error)]
15pub enum TranslationError {
16 #[error("invalid JSON: {0}")]
17 InvalidJson(#[from] serde_json::Error),
18
19 #[error("expected {expected} at {path}")]
20 InvalidType {
21 path: String,
22 expected: &'static str,
23 },
24
25 #[error("translation from {from} to {to} is not supported")]
26 UnsupportedTranslation { from: FormatId, to: FormatId },
27
28 #[error("lossy conversion rejected: {0}")]
29 LossyConversion(String),
30
31 #[error("invalid value at {path}: {message}")]
32 InvalidValue { path: String, message: String },
33
34 #[error("upstream reported a failed response: {error}")]
38 UpstreamFailure { error: serde_json::Value },
39 #[error("{0}")]
40 Other(String),
41}
42
43impl TranslationError {
44 pub const fn kind(&self) -> &'static str {
46 match self {
47 Self::InvalidJson(_) => "InvalidJson",
48 Self::InvalidType { .. } => "InvalidType",
49 Self::UnsupportedTranslation { .. } => "UnsupportedTranslation",
50 Self::LossyConversion(_) => "LossyConversion",
51 Self::InvalidValue { .. } => "InvalidValue",
52 Self::UpstreamFailure { .. } => "UpstreamFailure",
53 Self::Other(_) => "Other",
54 }
55 }
56
57 pub fn unsupported_role(path: impl Into<String>, value: &str) -> Self {
64 Self::InvalidValue {
65 path: path.into(),
66 message: format!(
67 "Invalid value: {value:?}. Supported message roles are \
68 system, developer, user, assistant, tool."
69 ),
70 }
71 }
72}