dynamo_async_openai/
error.rs

1// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Based on https://github.com/64bit/async-openai/ by Himanshu Neema
5// Original Copyright (c) 2022 Himanshu Neema
6// Licensed under MIT License (see ATTRIBUTIONS-Rust.md)
7//
8// Modifications Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES.
9// Licensed under Apache 2.0
10
11//! Errors originating from API calls, parsing responses, and reading-or-writing to the file system.
12use serde::{Deserialize, Serialize};
13
14#[derive(Debug, thiserror::Error)]
15pub enum OpenAIError {
16    /// Underlying error from reqwest library after an API call was made
17    #[error("http error: {0}")]
18    Reqwest(#[from] reqwest::Error),
19    /// OpenAI returns error object with details of API call failure
20    #[error("{0}")]
21    ApiError(ApiError),
22    /// Error when a response cannot be deserialized into a Rust type
23    #[error("failed to deserialize api response: {0}")]
24    JSONDeserialize(serde_json::Error),
25    /// Error on the client side when saving file to file system
26    #[error("failed to save file: {0}")]
27    FileSaveError(String),
28    /// Error on the client side when reading file from file system
29    #[error("failed to read file: {0}")]
30    FileReadError(String),
31    /// Error on SSE streaming
32    #[error("stream failed: {0}")]
33    StreamError(String),
34    /// Error from client side validation
35    /// or when builder fails to build request before making API call
36    #[error("invalid args: {0}")]
37    InvalidArgument(String),
38}
39
40/// OpenAI API returns error object on failure
41#[derive(Debug, Serialize, Deserialize, Clone)]
42pub struct ApiError {
43    pub message: String,
44    pub r#type: Option<String>,
45    pub param: Option<String>,
46    pub code: Option<String>,
47}
48
49impl std::fmt::Display for ApiError {
50    /// If all fields are available, `ApiError` is formatted as:
51    /// `{type}: {message} (param: {param}) (code: {code})`
52    /// Otherwise, missing fields will be ignored.
53    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54        let mut parts = Vec::new();
55
56        if let Some(r#type) = &self.r#type {
57            parts.push(format!("{}:", r#type));
58        }
59
60        parts.push(self.message.clone());
61
62        if let Some(param) = &self.param {
63            parts.push(format!("(param: {param})"));
64        }
65
66        if let Some(code) = &self.code {
67            parts.push(format!("(code: {code})"));
68        }
69
70        write!(f, "{}", parts.join(" "))
71    }
72}
73
74/// Wrapper to deserialize the error object nested in "error" JSON key
75#[derive(Debug, Deserialize, Serialize)]
76pub struct WrappedError {
77    pub error: ApiError,
78}
79
80pub(crate) fn map_deserialization_error(e: serde_json::Error, bytes: &[u8]) -> OpenAIError {
81    tracing::error!(
82        "failed deserialization of: {}",
83        String::from_utf8_lossy(bytes)
84    );
85    OpenAIError::JSONDeserialize(e)
86}