1use std::ffi::CStr;
2use std::fmt;
3
4use vllm_cpp_sys as ffi;
5
6#[derive(Clone, Debug, Eq, PartialEq)]
11#[non_exhaustive]
12pub enum HuggingFaceError {
13 InvalidInput { message: String },
15 CacheMiss { message: String },
17 Incomplete { message: String },
19 Hub { message: String },
21 Io { message: String },
23}
24
25impl fmt::Display for HuggingFaceError {
26 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27 match self {
28 Self::InvalidInput { message } => write!(f, "invalid Hugging Face model: {message}"),
29 Self::CacheMiss { message } => write!(f, "Hugging Face cache miss: {message}"),
30 Self::Incomplete { message } => {
31 write!(f, "incomplete Hugging Face snapshot: {message}")
32 }
33 Self::Hub { message } => write!(f, "Hugging Face Hub failure: {message}"),
34 Self::Io { message } => write!(f, "Hugging Face cache I/O failure: {message}"),
35 }
36 }
37}
38
39impl std::error::Error for HuggingFaceError {}
40
41#[derive(Clone, Debug, Eq, PartialEq)]
43#[non_exhaustive]
44pub enum Error {
45 AbiMismatch { expected: i32, actual: i32 },
47 InvalidArgument { message: String },
49 ModelLoad { message: String },
51 Runtime { message: String },
53 NativeUnknown { message: String },
55 UnknownStatus { status: u32, message: String },
57 InteriorNul { field: &'static str },
59 PathEncoding,
61 InvalidUtf8 { field: &'static str },
63 CallbackPanicked,
65 LogitsProcessorPanicked,
67 RequestCallbackThread { operation: &'static str },
69 InvalidConfiguration { message: String },
71 Json {
73 context: &'static str,
74 message: String,
75 },
76}
77
78impl fmt::Display for Error {
79 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80 match self {
81 Self::AbiMismatch { expected, actual } => {
82 write!(
83 f,
84 "vllm.cpp ABI mismatch: expected {expected}, found {actual}"
85 )
86 }
87 Self::InvalidArgument { message } => write!(f, "invalid argument: {message}"),
88 Self::ModelLoad { message } => write!(f, "model load failed: {message}"),
89 Self::Runtime { message } => write!(f, "vllm.cpp runtime failure: {message}"),
90 Self::NativeUnknown { message } => write!(f, "unknown native failure: {message}"),
91 Self::UnknownStatus { status, message } => {
92 write!(f, "unknown native status {status}: {message}")
93 }
94 Self::InteriorNul { field } => write!(f, "{field} contains an interior NUL byte"),
95 Self::PathEncoding => write!(f, "path cannot be represented by the native API"),
96 Self::InvalidUtf8 { field } => write!(f, "native {field} is not valid UTF-8"),
97 Self::CallbackPanicked => write!(f, "asynchronous request callback panicked"),
98 Self::LogitsProcessorPanicked => write!(f, "custom logits processor panicked"),
99 Self::RequestCallbackThread { operation } => {
100 write!(
101 f,
102 "cannot {operation} a request from its own callback thread"
103 )
104 }
105 Self::InvalidConfiguration { message } => {
106 write!(f, "invalid configuration: {message}")
107 }
108 Self::Json { context, message } => write!(f, "{context}: {message}"),
109 }
110 }
111}
112
113impl std::error::Error for Error {}
114
115pub(crate) fn status_result(status: ffi::vllm_status) -> Result<(), Error> {
116 if status == ffi::vllm_status_VLLM_OK {
117 return Ok(());
118 }
119
120 let message = unsafe {
123 let pointer = ffi::vllm_last_error();
124 if pointer.is_null() {
125 String::new()
126 } else {
127 CStr::from_ptr(pointer).to_string_lossy().into_owned()
128 }
129 };
130 let error = match status {
131 ffi::vllm_status_VLLM_ERR_INVALID_ARGUMENT => Error::InvalidArgument { message },
132 ffi::vllm_status_VLLM_ERR_MODEL_LOAD => Error::ModelLoad { message },
133 ffi::vllm_status_VLLM_ERR_RUNTIME => Error::Runtime { message },
134 ffi::vllm_status_VLLM_ERR_UNKNOWN => Error::NativeUnknown { message },
135 status => Error::UnknownStatus { status, message },
136 };
137 Err(error)
138}
139
140pub(crate) fn invalid_configuration(message: impl Into<String>) -> Error {
141 Error::InvalidConfiguration {
142 message: message.into(),
143 }
144}