ort_openrouter_cli/common/
error.rs1use core::fmt;
8
9extern crate alloc;
10
11#[repr(u8)]
12#[derive(Debug, Clone, Copy)]
13pub enum ErrorKind {
14 MissingApiKey = 1,
17 InvalidArguments,
19 ConfigParseFailed,
21 ConfigReadFailed,
23 MissingHomeDir,
24
25 HistoryMissing,
27 HistoryParseFailed,
28 HistoryReadFailed,
29 HistoryLookupFailed,
30
31 InvalidMessageSchema,
33
34 StdoutWriteFailed,
37 QueueDesync,
38 MissingUsageStats,
40 ResponseStreamError,
41 LastWriterError,
42
43 FileCreateFailed,
45 FileReadFailed,
46 FileWriteFailed,
47 FileStatFailed,
48 DirOpenFailed,
49
50 ThreadStackAllocFailed,
54 ThreadSpawnFailed,
56
57 DnsResolveFailed,
60 SocketCreateFailed,
62 SocketConnectFailed,
64 SocketReadFailed,
65 SocketWriteFailed,
66
67 UnexpectedEof,
69
70 ChunkedEofInSize,
74 ChunkedSizeReadError,
76 ChunkedInvalidSize,
77 ChunkedDataReadError,
79
80 HttpStatusError,
82 HttpConnectError,
83
84 TlsExpectedHandshakeRecord,
87 TlsExpectedServerHello,
88 TlsExpectedChangeCipherSpec,
90 TlsExpectedEncryptedRecords,
91 TlsBadHandshakeFragment,
92 TlsFinishedVerifyFailed,
93 TlsUnsupportedCipher,
94 TlsAlertReceived,
95 TlsRecordTooShort,
96 TlsHandshakeHeaderTooShort,
97 TlsHandshakeBodyTooShort,
98 TlsServerHelloTooShort,
99 TlsServerHelloSessionIdInvalid,
100 TlsServerHelloExtTooShort,
101 TlsExtensionHeaderTooShort,
102 TlsExtensionLengthInvalid,
103 TlsKeyShareServerHelloInvalid,
104 TlsServerGroupUnsupported,
105 TlsKeyShareLengthInvalid,
106 TlsServerNotTls13,
107 TlsMissingServerKey,
108
109 FormatError,
111 RateLimited,
112 Other,
113}
114
115pub type OrtResult<T> = Result<T, OrtError>;
116
117#[derive(Debug, Clone, Copy)]
118pub struct OrtError {
119 pub kind: ErrorKind,
120 pub context: &'static str,
121}
122
123pub fn ort_error(kind: ErrorKind, context: &'static str) -> OrtError {
124 OrtError { kind, context }
125}
126
127#[cfg(not(debug_assertions))]
129pub fn ort_from_err<E: core::fmt::Display>(
130 kind: ErrorKind,
131 context: &'static str,
132 _err: E,
133) -> OrtError {
134 ort_error(kind, context)
135}
136
137#[cfg(debug_assertions)]
139pub fn ort_from_err<E: core::fmt::Display>(
140 kind: ErrorKind,
141 context: &'static str,
142 err: E,
143) -> OrtError {
144 use crate::libc;
145 use alloc::ffi::CString;
146 use alloc::string::ToString;
147
148 let c_s = CString::new("\nERROR: ".to_string() + &err.to_string()).unwrap();
149 unsafe {
150 libc::write(2, c_s.as_ptr().cast(), c_s.count_bytes());
151 }
152
153 ort_error(kind, context)
154}
155
156impl OrtError {
157 #[cfg(debug_assertions)]
158 pub fn debug_print(&self) {
159 use crate::libc;
160 use alloc::ffi::CString;
161 use alloc::string::ToString;
162 let s = self.to_string();
163 let c_s = CString::new(s).unwrap();
164 unsafe {
165 libc::write(2, c_s.as_ptr().cast(), c_s.count_bytes());
166 }
167 }
168
169 #[cfg(not(debug_assertions))]
170 pub fn debug_print(&self) {}
171}
172
173impl core::error::Error for OrtError {}
174
175impl fmt::Display for OrtError {
176 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
177 write!(f, "{:?}: {}", self.kind, self.context)
178 }
179}
180
181impl From<core::fmt::Error> for OrtError {
182 fn from(err: core::fmt::Error) -> OrtError {
183 let _ = err;
185 ort_error(ErrorKind::FormatError, "")
186 }
187}
188
189pub trait Context<T, E> {
190 fn context(self, context: &'static str) -> Result<T, OrtError>;
192}
193
194impl<T, E> Context<T, E> for Result<T, E>
195where
196 E: Into<OrtError>,
197{
198 fn context(self, context: &'static str) -> OrtResult<T> {
200 match self {
201 Ok(ok) => Ok(ok),
202 Err(error) => {
203 let mut err: OrtError = error.into();
204 err.context = context;
205 Err(err)
206 }
207 }
208 }
209}