I have a package that invokes some user-defined code that can be error-prone. For the most part, I'd like to respect the user's choices for handling errors, e.g. debug-on-error. However, due to some layers of indirection and the use of closures, it can be hard to understand the context of the error from the backtrace alone because original function names are erased, so I'd like to enhance the error message to add some useful context information. How can this be done?

I've tried the solution in Rethrowing an error in Emacs Lisp, however, this loses the valuable backtrace information.

I also tried setting a custom debugger: Get backtrace from error programmatically but this interferes with the standard debugger and may not respect user's debugger choices.

1 Answer 1


Before I finished writing the question I found an acceptable solution, combining the (let ((debugger ...)) ...) approach with invoking the (debug) command, which should do the right thing according to debug-on-error. (debug) also supports passing some useful context about the error:

(defmacro autobuild--debug-with-context (context-string form)
  "Append CONTEXT-STRING to the debugger if an error is signalled on FORM."
  (declare (indent 1))
  `(let ((debugger (lambda (&rest _args)
                     (debug ,context-string))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.