Skip to content

Instantly share code, notes, and snippets.

@rmorrise
Created April 23, 2015 15:55
Show Gist options
  • Save rmorrise/df09bdf7d06344bffa1d to your computer and use it in GitHub Desktop.
Save rmorrise/df09bdf7d06344bffa1d to your computer and use it in GitHub Desktop.
Grails wrapped exception handling test
package wrapped.exception
class ExceptionCausingController {
def wrappingService
def index() {
wrappingService.serviceMethod()
}
}
package wrapped.exception
/**
* @author: rmorrise
*/
class NestedException extends RuntimeException {
NestedException(def message) {
super(message)
}
}
package wrapped.exception
import grails.transaction.Transactional
@Transactional
class NestedService {
def serviceMethod() {
throw new NestedException("Nested error")
}
}
package wrapped.exception
/**
* @author: rmorrise
*/
class WrappingException extends RuntimeException {
WrappingException(String message, Throwable cause) {
super(message, cause)
}
}
package wrapped.exception
import grails.transaction.Transactional
@Transactional
class WrappingService {
def nestedService
def serviceMethod() {
try {
nestedService.serviceMethod()
}
catch (NestedException e) {
throw new WrappingException("Error called nested service from wrapped service: ${e.message}", e)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment