title: How to handle grails exceptions in an ajax request?
body: <p>If I am making a regular request I can define handling of errors and exceptions in UrlMappings.groovy as in the following example:</p>
  
  <pre><code>    "403"(controller: 'error', action: 'index', params:['code':'403'])
      "404"(controller: 'error', action: 'index', params:['code':'404'])
      "500"(controller: 'error', action: 'index', params:['code':'500'])
      "500"(controller: 'home', action: 'noPrivileges', exception: IllegalOperationException)
  </code></pre>
  
  <p>I can then handle the errors in a specific controller and render a view of my choice e.g.</p>
  
  <pre><code>class ErrorController {
  
      def index = {
          render view: "/errors/myErrorPage", model: [code: params.code]
      }
  }
  </code></pre>
  
  <p>This works perfectly and whenever there is an error on server I get my error page.</p>
  
  <p>Now how can I achieve the very same behavior for ajax requests? I am making ajax requests either with grails <a href="http://grails.org/doc/latest/ref/Tags/remoteFunction.html" rel="nofollow">remoteFunction</a> or <a href="http://grails.org/doc/latest/ref/Tags/formRemote.html" rel="nofollow">formRemote</a>. Everytime when an exception occurs on the server, I want to execute the same piece of code in javascript (e.g. I want to show an alert with alert('There was an exception')).</p>
  
  <p>The above described approach does not work for ajax requests. I still get back an error and even though there is content of myErrorPage.gsp in the error, it is not printed into the html element which I specified with update parameter of formRemote or remoteFunction.</p>