Handling XML/JSON Errors in Rails
The first time I run into this, I was just feeling so upset. I wasnāt in a ārailsyā mood for a while, my colleague posted an incorrectly formatted xml to our staging server and told me he got 500. No way, I saidĀ ! Slap - in my face, nothing useful in the logs, it was happening ⦠My ruby-ninja sense came up with the followingĀ : // <![CDATA[ embedGist('181736.js'); // ]]> Of course it needs to check in a before filter for request.env['EXCEPTION'] to be re-raised if found ⦠Few months later, Iāve put together a plugin to handle XML/JSON parse errors and treat parsing exceptions like other raised errors, an example is more than 1024 wordsĀ : // <![CDATA[ embedGist('181762.js'); // ]]> Please realize, that even if exceptions are treated equal those related to request parameter parsing probably caused some parameters to be not available in your controller code (you obviously canāt access params[:user] when posting an invalid user XML snippet) and thus You should NOT recover from thoseĀ ! The primary intent of this plugin is to be able to customize the default HTTP 500 status returned by Rails. UPDATE: Starting Rails 2.3.5Ā ActiveSupport::JSON::ParseError is gone, Youāll have to use the JSON backendās parse error for rescuing (available at ActiveSupport::JSON.parse_error). UPDATE: As Rails 3.x did not really update the code that handles parameter parsing errors, the plugin has been reviewed to work with 3.x (and is threadsafe). See the plugin at http://github.com/kares/request_exception_handler.

















