use core::fmt;
extern crate alloc;
#[repr(u8)]
#[derive(Debug, Clone, Copy)]
pub enum ErrorKind {
MissingApiKey = 1,
InvalidArguments,
ConfigParseFailed,
ConfigReadFailed,
MissingHomeDir,
HistoryMissing,
HistoryParseFailed,
HistoryReadFailed,
HistoryLookupFailed,
InvalidMessageSchema,
StdoutWriteFailed,
QueueDesync,
MissingUsageStats,
ResponseStreamError,
LastWriterError,
FileCreateFailed,
FileReadFailed,
FileWriteFailed,
FileStatFailed,
DirOpenFailed,
ThreadStackAllocFailed,
ThreadSpawnFailed,
DnsResolveFailed,
SocketCreateFailed,
SocketConnectFailed,
SocketReadFailed,
SocketWriteFailed,
UnexpectedEof,
ChunkedEofInSize,
ChunkedSizeReadError,
ChunkedInvalidSize,
ChunkedDataReadError,
HttpStatusError,
HttpConnectError,
TlsExpectedHandshakeRecord,
TlsExpectedServerHello,
TlsExpectedChangeCipherSpec,
TlsExpectedEncryptedRecords,
TlsBadHandshakeFragment,
TlsFinishedVerifyFailed,
TlsUnsupportedCipher,
TlsAlertReceived,
TlsRecordTooShort,
TlsHandshakeHeaderTooShort,
TlsHandshakeBodyTooShort,
TlsServerHelloTooShort,
TlsServerHelloSessionIdInvalid,
TlsServerHelloExtTooShort,
TlsExtensionHeaderTooShort,
TlsExtensionLengthInvalid,
TlsKeyShareServerHelloInvalid,
TlsServerGroupUnsupported,
TlsKeyShareLengthInvalid,
TlsServerNotTls13,
TlsMissingServerKey,
FormatError,
RateLimited,
Other,
}
pub type OrtResult<T> = Result<T, OrtError>;
#[derive(Debug, Clone, Copy)]
pub struct OrtError {
pub kind: ErrorKind,
pub context: &'static str,
}
pub fn ort_error(kind: ErrorKind, context: &'static str) -> OrtError {
OrtError { kind, context }
}
#[cfg(not(debug_assertions))]
pub fn ort_from_err<E: core::fmt::Display>(
kind: ErrorKind,
context: &'static str,
_err: E,
) -> OrtError {
ort_error(kind, context)
}
#[cfg(debug_assertions)]
pub fn ort_from_err<E: core::fmt::Display>(
kind: ErrorKind,
context: &'static str,
err: E,
) -> OrtError {
use crate::libc;
use alloc::ffi::CString;
use alloc::string::ToString;
let c_s = CString::new("\nERROR: ".to_string() + &err.to_string()).unwrap();
unsafe {
libc::write(2, c_s.as_ptr().cast(), c_s.count_bytes());
}
ort_error(kind, context)
}
impl OrtError {
#[cfg(debug_assertions)]
pub fn debug_print(&self) {
use crate::libc;
use alloc::ffi::CString;
use alloc::string::ToString;
let s = self.to_string();
let c_s = CString::new(s).unwrap();
unsafe {
libc::write(2, c_s.as_ptr().cast(), c_s.count_bytes());
}
}
#[cfg(not(debug_assertions))]
pub fn debug_print(&self) {}
}
impl core::error::Error for OrtError {}
impl fmt::Display for OrtError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}: {}", self.kind, self.context)
}
}
impl From<core::fmt::Error> for OrtError {
fn from(err: core::fmt::Error) -> OrtError {
let _ = err;
ort_error(ErrorKind::FormatError, "")
}
}
pub trait Context<T, E> {
fn context(self, context: &'static str) -> Result<T, OrtError>;
}
impl<T, E> Context<T, E> for Result<T, E>
where
E: Into<OrtError>,
{
fn context(self, context: &'static str) -> OrtResult<T> {
match self {
Ok(ok) => Ok(ok),
Err(error) => {
let mut err: OrtError = error.into();
err.context = context;
Err(err)
}
}
}
}