mlua/error.rs
1//! Lua error handling.
2//!
3//! This module provides the [`Error`] type returned by all fallible `mlua` operations, together
4//! with extension traits for adapting Rust errors for use within Lua.
5
6use std::error::Error as StdError;
7use std::fmt;
8use std::io::Error as IoError;
9use std::net::AddrParseError;
10use std::result::Result as StdResult;
11use std::str::Utf8Error;
12use std::sync::Arc;
13
14use crate::private::Sealed;
15
16#[cfg(feature = "error-send")]
17type DynStdError = dyn StdError + Send + Sync;
18
19#[cfg(not(feature = "error-send"))]
20type DynStdError = dyn StdError;
21
22/// Error type returned by `mlua` methods.
23#[derive(Debug, Clone)]
24#[non_exhaustive]
25pub enum Error {
26 /// Syntax error while parsing Lua source code.
27 SyntaxError {
28 /// The error message as returned by Lua.
29 message: String,
30 /// `true` if the error can likely be fixed by appending more input to the source code.
31 ///
32 /// This is useful for implementing REPLs as they can query the user for more input if this
33 /// is set.
34 incomplete_input: bool,
35 },
36 /// Lua runtime error, aka `LUA_ERRRUN`.
37 ///
38 /// The Lua VM returns this error when a builtin operation is performed on incompatible types.
39 /// Among other things, this includes invoking operators on wrong types (such as calling or
40 /// indexing a `nil` value).
41 RuntimeError(String),
42 /// Lua memory error, aka `LUA_ERRMEM`
43 ///
44 /// The Lua VM returns this error when the allocator does not return the requested memory, aka
45 /// it is an out-of-memory error.
46 MemoryError(String),
47 /// Lua garbage collector error, aka `LUA_ERRGCMM`.
48 ///
49 /// The Lua VM returns this error when there is an error running a `__gc` metamethod.
50 #[cfg(any(feature = "lua53", feature = "lua52", doc))]
51 #[cfg_attr(docsrs, doc(cfg(any(feature = "lua53", feature = "lua52"))))]
52 GarbageCollectorError(String),
53 /// Potentially unsafe action in safe mode.
54 SafetyError(String),
55 /// Memory control is not available.
56 ///
57 /// This error can only happen when Lua state was not created by us and does not have the
58 /// custom allocator attached.
59 MemoryControlNotAvailable,
60 /// A mutable callback has triggered Lua code that has called the same mutable callback again.
61 ///
62 /// This is an error because a mutable callback can only be borrowed mutably once.
63 RecursiveMutCallback,
64 /// Either a callback or a userdata method has been called, but the callback or userdata has
65 /// been destructed.
66 ///
67 /// This can happen either due to to being destructed in a previous __gc, or due to being
68 /// destructed from exiting a `Lua::scope` call.
69 CallbackDestructed,
70 /// Not enough stack space to place arguments to Lua functions or return values from callbacks.
71 ///
72 /// Due to the way `mlua` works, it should not be directly possible to run out of stack space
73 /// during normal use. The only way that this error can be triggered is if a `Function` is
74 /// called with a huge number of arguments, or a Rust callback returns a huge number of return
75 /// values.
76 StackError,
77 /// Too many arguments to [`Function::bind`].
78 ///
79 /// [`Function::bind`]: crate::Function::bind
80 BindError,
81 /// Bad argument received from Lua (usually when calling a function).
82 ///
83 /// This error can help to identify the argument that caused the error
84 /// (which is stored in the corresponding field).
85 BadArgument {
86 /// Function that was called.
87 to: Option<String>,
88 /// Argument position (usually starts from 1).
89 pos: usize,
90 /// Argument name.
91 name: Option<String>,
92 /// Underlying error returned when converting argument to a Lua value.
93 cause: Arc<Error>,
94 },
95 /// A Lua value could not be converted to the expected Rust type.
96 FromLuaConversionError {
97 /// Name of the Lua type that could not be converted.
98 from: &'static str,
99 /// Name of the Rust type that could not be created.
100 to: String,
101 /// A string containing more detailed error information.
102 message: Option<String>,
103 },
104 /// [`Thread::resume`] was called on an unresumable coroutine.
105 ///
106 /// A coroutine is unresumable if its main function has returned or if an error has occurred
107 /// inside the coroutine. Already running coroutines are also marked as unresumable.
108 ///
109 /// [`Thread::status`] can be used to check if the coroutine can be resumed without causing this
110 /// error.
111 ///
112 /// [`Thread::resume`]: crate::Thread::resume
113 /// [`Thread::status`]: crate::Thread::status
114 CoroutineUnresumable,
115 /// An [`AnyUserData`] is not the expected type in a borrow.
116 ///
117 /// This error can only happen when manually using [`AnyUserData`], or when implementing
118 /// metamethods for binary operators. Refer to the documentation of [`UserDataMethods`] for
119 /// details.
120 ///
121 /// [`AnyUserData`]: crate::AnyUserData
122 /// [`UserDataMethods`]: crate::UserDataMethods
123 UserDataTypeMismatch,
124 /// An [`AnyUserData`] borrow failed because it has been destructed.
125 ///
126 /// This error can happen either due to to being destructed in a previous __gc, or due to being
127 /// destructed from exiting a `Lua::scope` call.
128 ///
129 /// [`AnyUserData`]: crate::AnyUserData
130 UserDataDestructed,
131 /// An [`AnyUserData`] immutable borrow failed.
132 ///
133 /// This error can occur when a method on a [`UserData`] type calls back into Lua, which then
134 /// tries to call a method on the same [`UserData`] type. Consider restructuring your API to
135 /// prevent these errors.
136 ///
137 /// [`AnyUserData`]: crate::AnyUserData
138 /// [`UserData`]: crate::UserData
139 UserDataBorrowError,
140 /// An [`AnyUserData`] mutable borrow failed.
141 ///
142 /// This error can occur when a method on a [`UserData`] type calls back into Lua, which then
143 /// tries to call a method on the same [`UserData`] type. Consider restructuring your API to
144 /// prevent these errors.
145 ///
146 /// [`AnyUserData`]: crate::AnyUserData
147 /// [`UserData`]: crate::UserData
148 UserDataBorrowMutError,
149 /// A [`MetaMethod`] operation is restricted (typically for `__gc` or `__metatable`).
150 ///
151 /// [`MetaMethod`]: crate::MetaMethod
152 MetaMethodRestricted(String),
153 /// A [`MetaMethod`] (eg. `__index` or `__newindex`) has invalid type.
154 ///
155 /// [`MetaMethod`]: crate::MetaMethod
156 MetaMethodTypeError {
157 /// Name of the metamethod.
158 method: String,
159 /// Passed value type.
160 type_name: &'static str,
161 /// A string containing more detailed error information.
162 message: Option<String>,
163 },
164 /// A [`RegistryKey`] produced from a different Lua state was used.
165 ///
166 /// [`RegistryKey`]: crate::RegistryKey
167 MismatchedRegistryKey,
168 /// A Rust callback returned `Err`, raising the contained `Error` as a Lua error.
169 CallbackError {
170 /// Lua call stack backtrace.
171 traceback: String,
172 /// Original error returned by the Rust code.
173 cause: Arc<Error>,
174 },
175 /// A Rust panic that was previously resumed, returned again.
176 ///
177 /// This error can occur only when a Rust panic resumed previously was recovered
178 /// and returned again.
179 PreviouslyResumedPanic,
180 /// Serialization error.
181 #[cfg(feature = "serde")]
182 #[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
183 SerializeError(String),
184 /// Deserialization error.
185 #[cfg(feature = "serde")]
186 #[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
187 DeserializeError(String),
188 /// A custom error.
189 ///
190 /// This can be used for returning user-defined errors from callbacks.
191 ///
192 /// Returning `Err(ExternalError(...))` from a Rust callback will raise the error as a Lua
193 /// error. The Rust code that originally invoked the Lua code then receives a `CallbackError`,
194 /// from which the original error (and a stack traceback) can be recovered.
195 ExternalError(Arc<DynStdError>),
196 /// An error with additional context.
197 WithContext {
198 /// A string containing additional context.
199 context: String,
200 /// Underlying error.
201 cause: Arc<Error>,
202 },
203}
204
205/// A specialized `Result` type used by `mlua`'s API.
206pub type Result<T> = StdResult<T, Error>;
207
208#[cfg(not(tarpaulin_include))]
209impl fmt::Display for Error {
210 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
211 match self {
212 Error::SyntaxError { message, .. } => write!(fmt, "syntax error: {message}"),
213 Error::RuntimeError(msg) => write!(fmt, "runtime error: {msg}"),
214 Error::MemoryError(msg) => {
215 write!(fmt, "memory error: {msg}")
216 }
217 #[cfg(any(feature = "lua53", feature = "lua52"))]
218 Error::GarbageCollectorError(msg) => {
219 write!(fmt, "garbage collector error: {msg}")
220 }
221 Error::SafetyError(msg) => {
222 write!(fmt, "safety error: {msg}")
223 }
224 Error::MemoryControlNotAvailable => {
225 write!(fmt, "memory control is not available")
226 }
227 Error::RecursiveMutCallback => write!(fmt, "mutable callback called recursively"),
228 Error::CallbackDestructed => write!(
229 fmt,
230 "a destructed callback or destructed userdata method was called"
231 ),
232 Error::StackError => write!(
233 fmt,
234 "out of Lua stack, too many arguments to a Lua function or too many return values from a callback"
235 ),
236 Error::BindError => write!(fmt, "too many arguments to Function::bind"),
237 Error::BadArgument { to, pos, name, cause } => {
238 if let Some(name) = name {
239 write!(fmt, "bad argument `{name}`")?;
240 } else {
241 write!(fmt, "bad argument #{pos}")?;
242 }
243 if let Some(to) = to {
244 write!(fmt, " to `{to}`")?;
245 }
246 write!(fmt, ": {cause}")
247 }
248 Error::FromLuaConversionError { from, to, message } => {
249 write!(fmt, "error converting Lua {from} to {to}")?;
250 match message {
251 None => Ok(()),
252 Some(message) => write!(fmt, " ({message})"),
253 }
254 }
255 Error::CoroutineUnresumable => write!(fmt, "coroutine is non-resumable"),
256 Error::UserDataTypeMismatch => write!(fmt, "userdata is not expected type"),
257 Error::UserDataDestructed => write!(fmt, "userdata has been destructed"),
258 Error::UserDataBorrowError => write!(fmt, "error borrowing userdata"),
259 Error::UserDataBorrowMutError => write!(fmt, "error mutably borrowing userdata"),
260 Error::MetaMethodRestricted(method) => write!(fmt, "metamethod {method} is restricted"),
261 Error::MetaMethodTypeError {
262 method,
263 type_name,
264 message,
265 } => {
266 write!(fmt, "metamethod {method} has unsupported type {type_name}")?;
267 match message {
268 None => Ok(()),
269 Some(message) => write!(fmt, " ({message})"),
270 }
271 }
272 Error::MismatchedRegistryKey => {
273 write!(fmt, "RegistryKey used from different Lua state")
274 }
275 Error::CallbackError { cause, traceback } => {
276 // Trace errors down to the root
277 let (mut cause, mut full_traceback) = (cause, None);
278 while let Error::CallbackError {
279 cause: cause2,
280 traceback: traceback2,
281 } = &**cause
282 {
283 cause = cause2;
284 full_traceback = Some(traceback2);
285 }
286 writeln!(fmt, "{cause}")?;
287 if let Some(full_traceback) = full_traceback {
288 let traceback = traceback.trim_start_matches("stack traceback:");
289 let traceback = traceback.trim_start().trim_end();
290 // Try to find local traceback within the full traceback
291 if let Some(pos) = full_traceback.find(traceback) {
292 write!(fmt, "{}", &full_traceback[..pos])?;
293 writeln!(fmt, ">{}", &full_traceback[pos..].trim_end())?;
294 } else {
295 writeln!(fmt, "{}", full_traceback.trim_end())?;
296 }
297 } else {
298 writeln!(fmt, "{}", traceback.trim_end())?;
299 }
300 Ok(())
301 }
302 Error::PreviouslyResumedPanic => {
303 write!(fmt, "previously resumed panic returned again")
304 }
305 #[cfg(feature = "serde")]
306 Error::SerializeError(err) => {
307 write!(fmt, "serialize error: {err}")
308 }
309 #[cfg(feature = "serde")]
310 Error::DeserializeError(err) => {
311 write!(fmt, "deserialize error: {err}")
312 }
313 Error::ExternalError(err) => err.fmt(fmt),
314 Error::WithContext { context, cause } => {
315 writeln!(fmt, "{context}")?;
316 write!(fmt, "{cause}")
317 }
318 }
319 }
320}
321
322impl StdError for Error {
323 fn source(&self) -> Option<&(dyn StdError + 'static)> {
324 match self {
325 // An error type with a source error should either return that error via source or
326 // include that source's error message in its own Display output, but never both.
327 // https://blog.rust-lang.org/inside-rust/2021/07/01/What-the-error-handling-project-group-is-working-towards.html
328 // Given that we include source to fmt::Display implementation for `CallbackError`, this call
329 // returns nothing.
330 Error::CallbackError { .. } => None,
331 Error::ExternalError(err) => err.source(),
332 Error::WithContext { cause, .. } => Self::source(cause),
333 _ => None,
334 }
335 }
336}
337
338impl Error {
339 /// Creates a new `RuntimeError` with the given message.
340 #[inline]
341 pub fn runtime<S: fmt::Display>(message: S) -> Self {
342 Error::RuntimeError(message.to_string())
343 }
344
345 /// Wraps an external error object.
346 #[inline]
347 pub fn external<T: Into<Box<DynStdError>>>(err: T) -> Self {
348 Error::ExternalError(err.into().into())
349 }
350
351 /// Attempts to downcast the external error object to a concrete type by reference.
352 pub fn downcast_ref<T>(&self) -> Option<&T>
353 where
354 T: StdError + 'static,
355 {
356 match self {
357 Error::ExternalError(err) => err.downcast_ref(),
358 Error::WithContext { cause, .. } => Self::downcast_ref(cause),
359 _ => None,
360 }
361 }
362
363 /// An iterator over the chain of nested errors wrapped by this Error.
364 pub fn chain(&self) -> impl Iterator<Item = &(dyn StdError + 'static)> {
365 Chain {
366 root: self,
367 current: None,
368 }
369 }
370
371 /// Returns the parent of this error.
372 #[doc(hidden)]
373 pub fn parent(&self) -> Option<&Error> {
374 match self {
375 Error::CallbackError { cause, .. } => Some(cause.as_ref()),
376 Error::WithContext { cause, .. } => Some(cause.as_ref()),
377 _ => None,
378 }
379 }
380
381 pub(crate) fn bad_self_argument(to: &str, cause: Error) -> Self {
382 Error::BadArgument {
383 to: Some(to.to_string()),
384 pos: 1,
385 name: Some("self".to_string()),
386 cause: Arc::new(cause),
387 }
388 }
389
390 #[inline]
391 pub(crate) fn from_lua_conversion(
392 from: &'static str,
393 to: impl ToString,
394 message: impl Into<Option<String>>,
395 ) -> Self {
396 Error::FromLuaConversionError {
397 from,
398 to: to.to_string(),
399 message: message.into(),
400 }
401 }
402}
403
404/// Trait for converting [`std::error::Error`] into Lua [`Error`].
405pub trait ExternalError {
406 fn into_lua_err(self) -> Error;
407}
408
409impl<E: Into<Box<DynStdError>>> ExternalError for E {
410 fn into_lua_err(self) -> Error {
411 Error::external(self)
412 }
413}
414
415/// Trait for converting [`std::result::Result`] into Lua [`Result`].
416pub trait ExternalResult<T> {
417 fn into_lua_err(self) -> Result<T>;
418}
419
420impl<T, E> ExternalResult<T> for StdResult<T, E>
421where
422 E: ExternalError,
423{
424 fn into_lua_err(self) -> Result<T> {
425 self.map_err(|e| e.into_lua_err())
426 }
427}
428
429/// Provides the `context` method for [`Error`] and `Result<T, Error>`.
430pub trait ErrorContext: Sealed {
431 /// Wraps the error value with additional context.
432 fn context<C: fmt::Display>(self, context: C) -> Self;
433
434 /// Wrap the error value with additional context that is evaluated lazily
435 /// only once an error does occur.
436 fn with_context<C: fmt::Display>(self, f: impl FnOnce(&Error) -> C) -> Self;
437}
438
439impl ErrorContext for Error {
440 fn context<C: fmt::Display>(self, context: C) -> Self {
441 let context = context.to_string();
442 match self {
443 Error::WithContext { cause, .. } => Error::WithContext { context, cause },
444 _ => Error::WithContext {
445 context,
446 cause: Arc::new(self),
447 },
448 }
449 }
450
451 fn with_context<C: fmt::Display>(self, f: impl FnOnce(&Error) -> C) -> Self {
452 let context = f(&self).to_string();
453 match self {
454 Error::WithContext { cause, .. } => Error::WithContext { context, cause },
455 _ => Error::WithContext {
456 context,
457 cause: Arc::new(self),
458 },
459 }
460 }
461}
462
463impl<T> ErrorContext for Result<T> {
464 fn context<C: fmt::Display>(self, context: C) -> Self {
465 self.map_err(|err| err.context(context))
466 }
467
468 fn with_context<C: fmt::Display>(self, f: impl FnOnce(&Error) -> C) -> Self {
469 self.map_err(|err| err.with_context(f))
470 }
471}
472
473impl From<AddrParseError> for Error {
474 fn from(err: AddrParseError) -> Self {
475 Error::external(err)
476 }
477}
478
479impl From<IoError> for Error {
480 fn from(err: IoError) -> Self {
481 Error::external(err)
482 }
483}
484
485impl From<Utf8Error> for Error {
486 fn from(err: Utf8Error) -> Self {
487 Error::external(err)
488 }
489}
490
491#[cfg(feature = "serde")]
492impl serde::ser::Error for Error {
493 fn custom<T: fmt::Display>(msg: T) -> Self {
494 Self::SerializeError(msg.to_string())
495 }
496}
497
498#[cfg(feature = "serde")]
499impl serde::de::Error for Error {
500 fn custom<T: fmt::Display>(msg: T) -> Self {
501 Self::DeserializeError(msg.to_string())
502 }
503}
504
505#[cfg(feature = "anyhow")]
506impl From<anyhow::Error> for Error {
507 fn from(err: anyhow::Error) -> Self {
508 match err.downcast::<Self>() {
509 Ok(err) => err,
510 Err(err) => Error::external(err),
511 }
512 }
513}
514
515struct Chain<'a> {
516 root: &'a Error,
517 current: Option<&'a (dyn StdError + 'static)>,
518}
519
520impl<'a> Iterator for Chain<'a> {
521 type Item = &'a (dyn StdError + 'static);
522
523 fn next(&mut self) -> Option<Self::Item> {
524 loop {
525 let error: Option<&dyn StdError> = match self.current {
526 None => {
527 self.current = Some(self.root);
528 self.current
529 }
530 Some(current) => match current.downcast_ref::<Error>()? {
531 Error::BadArgument { cause, .. }
532 | Error::CallbackError { cause, .. }
533 | Error::WithContext { cause, .. } => {
534 self.current = Some(&**cause);
535 self.current
536 }
537 Error::ExternalError(err) => {
538 self.current = Some(&**err);
539 self.current
540 }
541 _ => None,
542 },
543 };
544
545 // Skip `ExternalError` as it only wraps the underlying error
546 // without meaningful context
547 if let Some(Error::ExternalError(_)) = error?.downcast_ref::<Error>() {
548 continue;
549 }
550
551 return self.current;
552 }
553 }
554}
555
556#[cfg(test)]
557mod assertions {
558 use super::*;
559
560 #[cfg(not(feature = "error-send"))]
561 static_assertions::assert_not_impl_any!(Error: Send, Sync);
562 #[cfg(feature = "send")]
563 static_assertions::assert_impl_all!(Error: Send, Sync);
564}