Error Handling

Play Framework provides an error handling module which allows the application to capture the error message, introduce additional useful logic, such as sending a notification email to the administrator, and display a friendly error page.

Error Template

Error template will support the error views. The template is based on the Bootstrap Cover example.

Ctrl-click views and select New - HTML File.

Enter error_template.scala.html in the name field.

Double-click HTML 5 file.

If prompted, click Add to add the file to version control.

Browse to the project repository to view the file:

LineDrop Play/Scala Web Application - Error Template

Copy the contents of the file and paste them into error_template.scala.html replacing the original code.

Reference

Bootstrap Cover Template

Page Not Found

Add a view to be displayed if the Page Not Found error occurs. The page shows a friendly message and invites the user to subscribe.

Ctrl-click views and select New - HTML File.

Enter not_found.scala.html in the name field.

Double-click HTML 5 file.

If prompted, click Add to add the file to version control.

Browse to the project repository to view the file:

LineDrop Play/Scala Web Application - Not Found View

Copy the contents of the file and paste them into reset.scala.html replacing the original code.

Application Error

Add a view to be displayed if an application error occurs. The page shows a message explaining to the user that an error has occurred.

Ctrl-click views and select New - HTML File.

Enter error.scala.html in the name field.

Double-click HTML 5 file.

If prompted, click Add to add the file to version control.

Browse to the project repository to view the file:

LineDrop Play/Scala Web Application - Error View

Copy the contents of the file and paste them into error.scala.html replacing the original code.

Error Handler

Ctrl-click app and select New - Scala Class.

Enter Error in the name field and double-click Class.

If prompted, click Add to add the file to version control.

Browse to the project repository to view the file:

LineDrop Play/Scala Web Application - Error Handler

Copy and paste the contents into the new file replacing the original content.

The onClientError method intercepts the Page Not Found error and renders the Not Found view while providing the HTTP status code.

The onServerError method handles the application error. The error is logged, an email containing the error message is sent to the administrator.

Test

Application Response

To view the friendly Page Not Found page, browse to a non-existent page such as http://localhost:9000/somepage.

To view the friendly application error page, we need to create an application error. Open the Subscriber model and comment out the statement checking the empty subscribers map in the count_by_date method.

Using MongoDB Compass drop the subscribers collection. Click the ellipsis (...) next to subscribers in the left panel and select Drop Collection.

Type in the collection name and click DROP COLLECTION to confirm.

Browse to http://localhost:9000/subscribers, sign in if necessary. The application will crash and display the friendly error message.

Uncomment the if statement in the Subscriber Operations’ count_by_date method.

Email

Check your email to view the error notification.

Review

...

Depending on whether the request was made for a non-existing route or an internal application error has occurred, the Error Handler will invoke the onClientError or the onServerError method.

The onClientError method will display the Not Found view. The onServerError method will send a notification email to the administrator and the render the Error view.

Commit and Push Changes

Select VCS - Commit from the top menu.

Review the files and directories and enter the commit message.

Click the dropdown arrow on the Commit button and select Commit and Push.

Click Push to confirm.


Next: Application Maintenance