Skip to content

Instantly share code, notes, and snippets.

@jdtsmith
Last active August 25, 2023 02:31
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdtsmith/1fbcacfe677d74bbe510aec80ac0050c to your computer and use it in GitHub Desktop.
Save jdtsmith/1fbcacfe677d74bbe510aec80ac0050c to your computer and use it in GitHub Desktop.
Elisp: get stack trace for functions with suppressed errors (filter functions, post command hooks, etc.)
;;;; Power debugging
(defun my/reraise-error (func &rest args)
"Call function FUNC with ARGS and re-raise any error which occurs.
Useful for debugging post-command hooks and filter functions, which
normally have their errors suppressed."
(condition-case err
(apply func args)
((debug error) (signal (car err) (cdr err)))))
(defun toggle-debug-on-hidden-errors (func)
"Toggle hidden error debugging for function FUNC."
(interactive "a")
(cond
((advice-member-p #'my/reraise-error func)
(advice-remove func #'my/reraise-error)
(message "Debug on hidden errors disabled for %s" func))
(t
(advice-add func :around #'my/reraise-error)
(message "Debug on hidden errors enabled for %s" func))))
@jdtsmith
Copy link
Author

jdtsmith commented Feb 9, 2023

A very common source of "automatically caught and hidden errors" are process filter functions and post-command hooks. Many modes use these, and they can have subtle bugs. It can be frustrating to both users and mode developers if they can't reproduce your problem, and you can't get them a stack-trace.

Luckily, there is a nice way to re-enable such hidden errors, so that debug-on-error functions correctly. When you get one of those pesky "Error occurred in process filter function..." messages, just M-x toggle-debug-on-hidden-error, pick the likely function, make sure debug-on-error is on, and repeat whatever you did that lead to the error. Then toggle it back off.

@minad
Copy link

minad commented Feb 9, 2023

See also https://github.com/minad/vertico#debugging-vertico. The Vertico post-command-hook does not have any subtle bugs but some completion tables may have them. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment