Send 500 status code on debug view of errors. Check for JSON header as well. Fixes #1285.