New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Finer-grain control of exception behaviour in view renderers #1820
Conversation
I think it would be worthwhile to have a test in |
@nickbabcock I added a test/example to e2e on overriding the standard 503 response on missing mustache view with a 404 response. |
@@ -82,12 +76,9 @@ public void writeTo(View t, | |||
} | |||
} | |||
throw new ViewRenderException("Unable to find a renderer for " + t.getTemplateName()); | |||
} catch (Exception e) { | |||
} catch (ViewRenderException e) { | |||
LOGGER.error("Template Error", e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My preference would be to format the log message in my own ExceptionMapper, so could this go into the ExceptionMapper as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense. I've removed the logging from ViewMessageBodyWriter
and added the logging code to ViewRenderExceptionMapper
to preserve the original exception logging behavior.
The pull request looks good to me. I don't think the code coverage is so important, what matters you provided relevant tests. And thank you very much for a very detailed documentation for |
This PR is one possible implementation of issue #1811. In short, I am standardizing the exception thrown by each
ViewRenderer
implementation asViewRenderException
, which is then caught and rethrown asWebApplicationException
byViewMessageBodyWriter
.To preserve previous behavior of returning an HTML error response on render exceptions, I have added a new
ViewRenderExceptionMapper
class which should be registered. Consequently I am still changing the original behavior by adding this registration requirement. Let me know if this is OK from backward-compatibility standpoint - if needed we can register the mapper inViewBundle
, although I don't know how I can register another exception mapper afterwards if I want custom behaviour. This also requires change to the documentation.Otherwise, one can choose to provide their own
ExceptionMapper
to catchWebApplicationException
and check that the cause isViewRenderException
, then do whatever is needed with its cause in the form of an exception thrown by the rendering code (Viewmarker, Mustache, etc.)I tried updating the tests to verify both cases of with and without
ViewRenderExceptionMapper
, but I can't seem to get registration in each test case working like this:target("/test/bad").regsiter(new ViewRenderExceptionMapper()).request().get(String.class);
I had to register the mapper in
configure()
. Please let me know if you have any solution to this so I can increase the test coverage.Thanks.