rescue all errors and return the proper format

This rescues all errors and returns a proper JSON response. Fixes #2833.