[#2171] Action context access from error templates for routes-defined 404 actions #1231
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Lighthouse ticket
https://play.lighthouseapp.com/projects/57987-play-framework/tickets/2171
Framework version
1.5.3
Platform
Ubuntu 18.04, Oracle JDK 1.8.0_201
Description
For 404 actions declared in the routes file, action context items (e.g. controllers.Secure.Security.isConnected()) are not available to error templates and exceptions are thrown when they try to access them, resulting in HTTP 500 responses instead of HTTP 404 ones.
Without access to the session, HTTP 404 pages become more limited than other pages. This may cause the web app not be consistent across all pages.
Example: all the pages in a web app feature a navbar that displays "log in" if the user is logged out or "log out" if the user is logged in based on controllers.Secure.Security.isConnected(). The navbar may also display links to different web app actions depending on whether the user is logged in or not. All works well while a logged in user navigates across different pages, but as soon as a routes-defined 404 page is accessed, the navbar cannot be displayed like the other pages, cause there's no access to session information.
Actual behaviour reproduction steps
Create a brand new Play! 1.5.3 app
Add the secure module dependency (play -> secure) in conf/dependencies.yml and execute play dependencies
Include the following code in app/views/errors/404.html and app/views/main.html:
* /notfound 404
Start the app
Open a browser at the root URL (http://localhost:9000)
The returned page successfully displays isConnected/Connected
Point the browser at http://localhost:9000/notfound
An HTTP 500 is returned and logs show a NPE:
Expected behaviour
Session information should be available everywhere so web apps can be consistent across all pages.
Technical details
In class PlayHandler, the action context is reset in the init method, making it unavailable in templates.