jquants_api_client/
error.rs

1//! Custom error type module.
2
3use serde::Deserialize;
4use thiserror::Error;
5
6use crate::api::shared::responses::error_response::JQuantsErrorResponse;
7
8/// Common Error response
9#[derive(Debug, Deserialize)]
10pub struct ErrorResponse {
11    /// HTTP status code
12    pub status_code: u16,
13    /// Error message
14    pub error_message: String,
15}
16
17impl std::fmt::Display for ErrorResponse {
18    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19        write!(
20            f,
21            "Status code: {}, Error message: {}",
22            self.status_code, self.error_message
23        )
24    }
25}
26
27impl std::error::Error for ErrorResponse {}
28
29/// Custom error type for JQuants API client.
30/// This is a simple enum that wraps the reqwest::Error and ErrorResponse types.
31#[derive(Error, Debug)]
32pub enum JQuantsError {
33    /// Invalid credentials provided.
34    #[error("Invalid credentials provided. Status code: {status_code}, Body: {body}")]
35    InvalidCredentials {
36        /// HTTP status code
37        status_code: u16,
38
39        /// The error response
40        body: JQuantsErrorResponse,
41    },
42
43    /// Id token is invalid or expired.
44    #[error("ID token is invalid or expired. Status code: {status_code}, Message: {body}")]
45    IdTokenInvalidOrExpired {
46        /// HTTP status code
47        status_code: u16,
48
49        /// The error response
50        body: JQuantsErrorResponse,
51    },
52
53    /// Status code is 400 ~ 599. Response format is JQuants error response.
54    #[error("API error occurred. Status code: {status_code}, Message: {body}")]
55    ApiError {
56        /// HTTP status code
57        status_code: u16,
58
59        /// The error response. This is a JQuants error response format.
60        body: JQuantsErrorResponse,
61    },
62
63    /// Response format is not JQuants error response.
64    #[error("Invalid response format. Status code: {status_code}, Response body: {body}")]
65    InvalidResponseFormat {
66        /// HTTP status code
67        status_code: u16,
68
69        /// Response body
70        body: String,
71    },
72
73    /// HTTP request error
74    #[error("HTTP request error: {0}")]
75    ReqwestError(#[from] reqwest::Error),
76
77    /// Bug error. This should never happen.
78    #[error("BUG: {0}. Please report this issue.")]
79    BugError(String),
80}