1use std::ffi::CString;
22use std::fmt;
23
24pub(crate) const REGISTRATION_FAILURE_HINT: &str =
47 "the C API reports no reason. The usual cause is a name DuckDB will not merge: an \
48 aggregate cannot reuse any existing function or macro name (including its own earlier \
49 registration, e.g. on a retried LOAD), and a scalar cannot take an aggregate's or a \
50 macro's name (the built-in `list_sum` is a macro) — check `SELECT function_name, \
51 function_type FROM duckdb_functions() WHERE function_name = '<name>'`. Otherwise a \
52 parameter or return type is invalid, or a required callback was never set.";
53
54#[derive(Debug, Clone, PartialEq, Eq)]
76pub struct ExtensionError {
77 message: String,
78}
79
80impl ExtensionError {
81 #[inline]
92 pub fn new(message: impl Into<String>) -> Self {
93 Self {
94 message: message.into(),
95 }
96 }
97
98 #[inline]
110 pub fn from_error<E: std::error::Error>(e: E) -> Self {
111 Self {
112 message: e.to_string(),
113 }
114 }
115
116 #[must_use]
134 pub fn to_c_string(&self) -> CString {
135 crate::callback::message_to_c_string(&self.message)
136 }
137
138 #[must_use]
149 #[inline]
150 pub fn as_str(&self) -> &str {
151 &self.message
152 }
153}
154
155impl fmt::Display for ExtensionError {
156 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
157 f.write_str(&self.message)
158 }
159}
160
161impl std::error::Error for ExtensionError {}
162
163impl From<&str> for ExtensionError {
164 #[inline]
165 fn from(s: &str) -> Self {
166 Self::new(s)
167 }
168}
169
170impl From<String> for ExtensionError {
171 #[inline]
172 fn from(s: String) -> Self {
173 Self { message: s }
174 }
175}
176
177impl From<Box<dyn std::error::Error>> for ExtensionError {
178 #[inline]
179 fn from(e: Box<dyn std::error::Error>) -> Self {
180 Self {
181 message: e.to_string(),
182 }
183 }
184}
185
186impl From<Box<dyn std::error::Error + Send + Sync>> for ExtensionError {
187 #[inline]
188 fn from(e: Box<dyn std::error::Error + Send + Sync>) -> Self {
189 Self {
190 message: e.to_string(),
191 }
192 }
193}
194
195impl From<std::io::Error> for ExtensionError {
196 #[inline]
197 fn from(e: std::io::Error) -> Self {
198 Self {
199 message: e.to_string(),
200 }
201 }
202}
203
204impl From<std::ffi::NulError> for ExtensionError {
205 #[inline]
206 fn from(e: std::ffi::NulError) -> Self {
207 Self {
208 message: e.to_string(),
209 }
210 }
211}
212
213impl From<std::fmt::Error> for ExtensionError {
214 #[inline]
215 fn from(e: std::fmt::Error) -> Self {
216 Self {
217 message: e.to_string(),
218 }
219 }
220}
221
222pub type ExtResult<T> = Result<T, ExtensionError>;
224
225#[cfg(test)]
226mod tests {
227 use super::*;
228
229 #[test]
230 fn new_creates_with_message() {
231 let err = ExtensionError::new("test error");
232 assert_eq!(err.to_string(), "test error");
233 assert_eq!(err.as_str(), "test error");
234 }
235
236 #[test]
237 fn from_str() {
238 let err = ExtensionError::from("from str");
239 assert_eq!(err.message, "from str");
240 }
241
242 #[test]
243 fn from_string() {
244 let s = String::from("from String");
245 let err = ExtensionError::from(s);
246 assert_eq!(err.message, "from String");
247 }
248
249 #[test]
250 fn from_error_wraps_display() {
251 let parse_err = "abc".parse::<i32>().unwrap_err();
252 let err = ExtensionError::from_error(parse_err);
253 assert_ne!(err.message, "");
254 }
255
256 #[test]
257 fn to_c_string_normal() {
258 let err = ExtensionError::new("hello world");
259 let cstr = err.to_c_string();
260 assert_eq!(cstr.to_str().unwrap(), "hello world");
261 }
262
263 #[test]
264 fn to_c_string_with_null_byte() {
265 let err = ExtensionError::new("before\0after");
267 let cstr = err.to_c_string();
268 assert_eq!(cstr.to_str().unwrap(), "before?after");
269 }
270
271 #[test]
272 fn to_c_string_empty() {
273 let err = ExtensionError::new("");
274 let cstr = err.to_c_string();
275 assert_eq!(cstr.to_str().unwrap(), "");
276 }
277
278 #[test]
279 fn display_impl() {
280 let err = ExtensionError::new("display test");
281 let s = format!("{err}");
282 assert_eq!(s, "display test");
283 }
284
285 #[test]
286 fn debug_impl() {
287 let err = ExtensionError::new("debug");
288 let s = format!("{err:?}");
289 assert!(s.contains("debug"));
290 }
291
292 #[test]
293 fn clone_eq() {
294 let err1 = ExtensionError::new("clone test");
295 let err2 = err1.clone();
296 assert_eq!(err1, err2);
297 }
298
299 #[test]
300 fn from_box_dyn_error() {
301 let boxed: Box<dyn std::error::Error> = "abc".parse::<i32>().unwrap_err().into();
302 let err = ExtensionError::from(boxed);
303 assert_ne!(err.message, "");
304 }
305
306 #[test]
307 fn question_mark_operator_with_str() {
308 fn fails() -> Result<(), ExtensionError> {
309 Err("explicit error")?;
310 Ok(())
311 }
312 assert_eq!(fails().unwrap_err().as_str(), "explicit error");
313 }
314
315 #[test]
316 fn from_io_error() {
317 let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
318 let err = ExtensionError::from(io_err);
319 assert_eq!(err.as_str(), "file not found");
320 }
321
322 #[test]
323 fn question_mark_with_io_error() {
324 fn fails() -> Result<(), ExtensionError> {
325 Err(std::io::Error::other("runtime init failed"))?;
326 Ok(())
327 }
328 assert_eq!(fails().unwrap_err().as_str(), "runtime init failed");
329 }
330
331 #[test]
332 fn from_nul_error() {
333 let nul_err = std::ffi::CString::new("hello\0world").unwrap_err();
334 let err = ExtensionError::from(nul_err);
335 assert_ne!(err.as_str(), "");
336 }
337
338 #[test]
339 fn from_fmt_error() {
340 let fmt_err = std::fmt::Error;
341 let err = ExtensionError::from(fmt_err);
342 assert_ne!(err.as_str(), "");
343 }
344
345 #[test]
346 fn to_c_string_leading_null_byte() {
347 let err = ExtensionError::new("\0trailing");
348 let cstr = err.to_c_string();
349 assert_eq!(cstr.to_str().unwrap(), "?trailing");
350 }
351
352 #[test]
353 fn to_c_string_multiple_null_bytes() {
354 let err = ExtensionError::new("first\0second\0third");
355 let cstr = err.to_c_string();
356 assert_eq!(cstr.to_str().unwrap(), "first?second?third");
357 }
358
359 #[test]
360 fn from_box_dyn_error_send_sync() {
361 let boxed: Box<dyn std::error::Error + Send + Sync> =
362 "abc".parse::<i32>().unwrap_err().into();
363 let err = ExtensionError::from(boxed);
364 assert_ne!(err.message, "");
365 }
366
367 #[test]
368 fn ext_result_alias() {
369 let ok_val: ExtResult<i32> = Ok(42);
371 assert!(ok_val.is_ok());
372
373 let err_val: ExtResult<i32> = Err(ExtensionError::new("fail"));
374 assert!(err_val.is_err());
375 }
376}