Skip to content

Instantly share code, notes, and snippets.

@tobia
Created July 2, 2015 12:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobia/6c43c1e52f08442cafd8 to your computer and use it in GitHub Desktop.
Save tobia/6c43c1e52f08442cafd8 to your computer and use it in GitHub Desktop.
Simple try & catch taglib for Grails GSP views.
// Simple try & catch for GSP views.
//
// Usage:
//
// <g:try>
// view snippet that needs to be checked
// </g:try>
// <g:catch>
// alternative view code in case of errors;
// the exception is available as ${exception}
// </g:catch>
//
class TryCatchTagLib {
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
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment