1

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.

erjoalgo
  • 853
  • 1
  • 5
  • 18

1 Answers1

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))))
     ,form))
erjoalgo
  • 853
  • 1
  • 5
  • 18