nautilus_okx/http/error.rs
1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3// https://nautechsystems.io
4//
5// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6// You may not use this file except in compliance with the License.
7// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Error structures and enumerations for the OKX integration.
17//!
18//! The JSON error schema is described in the OKX documentation under
19//! *REST API > Error Codes* – <https://www.okx.com/docs-v5/en/#error-codes>.
20//! The types below mirror that structure and are reused across the entire
21//! crate.
22
23use nautilus_network::http::HttpClientError;
24use reqwest::StatusCode;
25use serde::Deserialize;
26use thiserror::Error;
27
28/// Represents a build error for query parameter validation.
29#[derive(Debug, Error)]
30pub enum BuildError {
31 /// Missing required instrument ID.
32 #[error("Missing required instrument ID")]
33 MissingInstId,
34 /// Missing required bar interval.
35 #[error("Missing required bar interval")]
36 MissingBar,
37 /// Both after and before cursors specified.
38 #[error("Cannot specify both 'after' and 'before' cursors")]
39 BothCursors,
40 /// Invalid time range: after_ms should be greater than before_ms.
41 #[error(
42 "Invalid time range: after_ms ({after_ms}) must be greater than before_ms ({before_ms})"
43 )]
44 InvalidTimeRange { after_ms: i64, before_ms: i64 },
45 /// Cursor timestamp is in nanoseconds (> 13 digits).
46 #[error("Cursor timestamp appears to be in nanoseconds (> 13 digits)")]
47 CursorIsNanoseconds,
48 /// Limit exceeds maximum allowed value.
49 #[error("Limit exceeds maximum of 300")]
50 LimitTooHigh,
51}
52
53/// Represents the JSON structure of an error response returned by the OKX API.
54#[derive(Clone, Debug, Deserialize)]
55pub struct OKXErrorResponse {
56 /// The top-level error object included in the OKX error response.
57 pub error: OKXErrorMessage,
58}
59
60/// Contains the specific error details provided by the OKX API.
61#[derive(Clone, Debug, Deserialize)]
62pub struct OKXErrorMessage {
63 /// A human-readable explanation of the error condition.
64 pub message: String,
65 /// A short identifier or category for the error, as returned by OKX.
66 pub name: String,
67}
68
69/// A typed error enumeration for the OKX HTTP client.
70#[derive(Debug, Error)]
71pub enum OKXHttpError {
72 /// Error variant when credentials are missing but the request is authenticated.
73 #[error("Missing credentials for authenticated request")]
74 MissingCredentials,
75 /// Errors returned directly by OKX (non-zero code).
76 #[error("OKX error {error_code}: {message}")]
77 OkxError { error_code: String, message: String },
78 /// Failure during JSON serialization/deserialization.
79 #[error("JSON error: {0}")]
80 JsonError(String),
81 /// Parameter validation error.
82 #[error("Parameter validation error: {0}")]
83 ValidationError(String),
84 /// Wrapping the underlying HttpClientError from the network crate.
85 #[error("Network error: {0}")]
86 HttpClientError(#[from] HttpClientError),
87 /// Any unknown HTTP status or unexpected response from OKX.
88 #[error("Unexpected HTTP status code {status}: {body}")]
89 UnexpectedStatus { status: StatusCode, body: String },
90}
91
92impl From<String> for OKXHttpError {
93 fn from(error: String) -> Self {
94 OKXHttpError::ValidationError(error)
95 }
96}
97
98// Allow use of the `?` operator on `serde_json` results inside the HTTP
99// client implementation by converting them into our typed error.
100impl From<serde_json::Error> for OKXHttpError {
101 fn from(error: serde_json::Error) -> Self {
102 OKXHttpError::JsonError(error.to_string())
103 }
104}