title: Overriding Grails uncaughtExceptionHandler
body: <p>I am using Grails 2.3 to create a REST API, and I want a better way to handle rendering error responses as a JSON response. What is have currently is a package of exceptions that all have a toJSON method. So all my controller methods wrap everything in the same try/catch blocks and render any error responses the same way.</p>
  
  <p>What I really want is to have an uncaught exception handler that does all this without having to wrap everything in try/catch. So if there's an uncaught exception, it'll automatically convert the exception into JSON, set the appropriate status code, and write and flush the response.</p>
  
  <p>Given all that, here's the actual question:
  So the first question is: is this a dumb idea? </p>
  
  <p>Since Grails handles all the thread pooling, I <em>think</em> I would need to register my handler at the beginning of every api call. Should I do this in the beforeInterceptor closure of each controller? </p>
  
  <p>Also, how would I be able to access the response object from my handler in order to be able to write the proper response?</p>
  
  <p>Even if someone could point me to the relevant grails classes in the docs, that would be a big help. I searched and could not find the classes where grails does this to see if I can use their existing classes instead of writing my own.</p>