Skip to main content
edited tags
Link
Drew
  • 79.1k
  • 10
  • 123
  • 257
Source Link
legoscia
  • 6.2k
  • 31
  • 54

Get backtrace from error programmatically

If an error is signalled in Emacs Lisp code, and debug-on-error is t, I get a backtrace buffer that makes it easy to figure out where the error occurred. However, for errors that occur while processing a response from the network asynchronously, it would be annoying to have the backtrace buffer pop up, so I'd rather catch the error with condition-case and log it.

So when I'm handling an error in condition-case, is there a way to get access to the backtrace at the point of the error? Calling the backtrace function gets the backtrace of the code inside the handler, which is not what I'm looking for.

(condition-case e
    (do-something-that-might-fail)
  (error
    (message "%s"
             ;; This gets the wrong backtrace!
             (with-temp-buffer
               (let ((standard-output (current-buffer)))
                 (backtrace)
                 (buffer-string))))))