Created
March 13, 2012 21:04
-
-
Save wbotelhos/2031577 to your computer and use it in GitHub Desktop.
Ajax Exception Interceptor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Intercepts | |
public class AjaxExceptionInterceptor implements Interceptor { | |
private final HttpServletRequest request; | |
private final Result result; | |
public AjaxExceptionInterceptor(HttpServletRequest request, Result result) { | |
this.request = request; | |
this.result = result; | |
} | |
public boolean accepts(ResourceMethod method) { | |
return request.getHeader("accept").contains("application/json"); | |
} | |
public void intercept(InterceptorStack stack, ResourceMethod method, Object resourceInstance) { | |
try { | |
stack.next(method, resourceInstance); | |
} catch (Exception e) { | |
result.use(Results.http()).sendError(500, e.getCause().getMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment