Showing posts with label try-catch-taglib. Show all posts
Showing posts with label try-catch-taglib. Show all posts

Thursday, November 30, 2017

Simple try & catch TagLib | Tag Library for Grails GSP views

First you have to create a TagLib as below:









With following contents:


package com.pkm.taglib

class TryCatchTagLib {
    static namespace = "ui"

    def Try = { attrs, body ->
        try {
            out << body()
            request.exception = null
        }
        catch (Throwable e) {
            request.exception = e
        }
    }

    def Catch = { attrs, body ->
        if (request.exception) {
            out << body(exception: request.exception)
            request.exception = null
        }
    }
}


And use of try-catch as below:


<ui:try>
    SOME WORK I AM TRYING
</ui:try>
<ui:catch>
    GOT EXCEPTION=${exception}
</ui:catch>