Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Grails Goodness: Exception Methods in Controllers

DZone's Guide to

Grails Goodness: Exception Methods in Controllers

· DevOps Zone
Free Resource

The DevOps Zone is brought to you in partnership with Sonatype Nexus. The Nexus Suite helps scale your DevOps delivery with continuous component intelligence integrated into development tools, including Eclipse, IntelliJ, Jenkins, Bamboo, SonarQube and more. Schedule a demo today

Since Grails 2.3 we can define exception methods in our controllers to handle exceptions raised by code invoked in the action methods of the controllers. Normally we would write a try/catch statement to handle an exception or let it continue up the stack until a 500 error page is shown. But with exception methods we can write code to handle exceptions in a controller without a try/catch statement. An exception method should define the type of exception it handles as the method argument. We can have multiple exception methods for different exception types. Also subclasses of a controller will use the exception methods if applicable.

In the following controller we have a couple of action methods: index and show. And we have two exception methods: connectException andnotFoundException. The connectException method has a single argument of type ConnectException. This means that any code in the controller that will raise a ConnectException will be handled by this method. And any ResourceNotFoundException thrown in the controller will be handled by the notFoundException method, because the argument type is ResourceNotFoundException.

package com.mrhaki.grails

class SampleController {

    /**
     * Service with methods that are invoked
     * from the controller action methods.
     */ 
    ExternalService externalService

    //--------------------------------------------
    // Action methods:
    //--------------------------------------------

    /** Index action method */
    def index() { 

        // These method calls could throw a ConnectException.
        // If the ConnectException occurs then the 
        // connectException(ConnectException) method is
        // invoked and that method will handle the 
        // request further.
        final all = externalService.all(params)
        final total = externalService.count()
        
        [items: all, totalCount: total]
    }

    /** Show action method */
    def show(final Long id) {

        // This method can throw a ConnectException
        // or ResourceNotFoundException. 
        // If the ResourceNotFoundException is thrown
        // the request is further handled by 
        // the notFoundException(ResourceNotFoundException)
        // method.
        final item = externalService.get(id)
        [item: item]
    }


    //--------------------------------------------
    // Exception methods:
    //--------------------------------------------

    /**
     * If any method in this controller invokes code that
     * will throw a ConnectException then this method
     * is invoked.
     */
    def connectException(final ConnectException exception) {
        logException exception
        render view: 'error', model: [exception: exception]
    }

    /**
     * If any method in this controller invokes code that
     * will throw a ResourceNotFoundException then this method
     * is invoked.
     */
    def notFoundException(final ResourceNotFoundException exception) {
        logException exception
        render view: 'notFound',  model: [id: params.id, exception: exception]        
    }


    /** Log exception */
    private void logException(final Exception exception) {
        log.error "Exception occurred. ${exception?.message}", exception
    }

}

Code written with Grails 2.4.0.

The DevOps Zone is brought to you in partnership with Sonatype Nexus. Use the Nexus Suite to automate your software supply chain and ensure you're using the highest quality open source components at every step of the development lifecycle. Get Nexus today

Topics:

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}