Exercise 24: Handling Exceptions - jkneal/spring-angular-train GitHub Wiki

Goals

Learn how to handle exceptions with Spring MVC

Instructions

  1. In edu.train.product, create a runtime exception named 'ProductNotFoundException'. Map this exception to the Http status 404 and the message "Product Not Found"

  2. Now open edu.train.product.InventoryController. Modify the method `deleteProductById' to use the exception (you created in step 1) if the product does not exist (note, you will not longer need to use ResponseEntity)

  3. In edu.train, create a new class named 'ExceptionHandlers'. This class should apply controller advice (for all controllers). Within this class, add a method named 'handleAccessException' that handles exceptions of type edu.train.AccessException. The method should return the logical view name 'access-error'

VERIFICATION

  1. Start the training application and go to the app home page
  2. Click on the link 'Ex 24 Delete Invalid'. Verify you receive an alert with the message "Error in request: status code: 404"
  3. Now click on the link 'Ex 24 Access'. Verify you see page with the message "Sorry. You do not have access to the requested page."