This logic matches that of `distributed/views.py:handler_500`; did this while debugging.