Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass error messages between Grails controllers?

I'm trying to pass an error message from a grails controller to a grails error controller in order to display an error message in the HTTP response, but I'm not sure what parameter is holding the error message in the error controller.

URLMappings.groovy

All 500 errors are mapped to ErrorsController

"500"(controller: "errors", action: "serverError")

GenericController

def {
  try{
    //do some work
  }catch(Exception e){
    response.sendError(500, e.getMessage())
  }
}

ErrorsController

def serverError = {

  render( how can I access the exception details here?? )

}

I need to access the exception in the ErrorsController so I can output it to the HTTP response.

like image 954
raffian Avatar asked Nov 18 '11 16:11

raffian


1 Answers

The usual way to pass short informational messages between controllers is to place it in the flash scope. For example:

def myAction = {
    try {
        ...
    } catch (Exception e) {
        flash.message = e.message
        response.sendError(500)
    }
}

In this particular case, though, why are you catching the exception? If you let the exception fall through, grails will automatically generate a server error and call the "500" mapping. In your error controller, the exception will be available as request.exception.

like image 182
ataylor Avatar answered Nov 15 '22 04:11

ataylor