Exercise 24: Handling Exceptions - jkneal/spring-angular-train GitHub Wiki
Goals
Learn how to handle exceptions with Spring MVC
Instructions
-
In
edu.train.product, create a runtime exception named 'ProductNotFoundException'. Map this exception to the Http status 404 and the message "Product Not Found" -
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) -
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 typeedu.train.AccessException. The method should return the logical view name 'access-error'
VERIFICATION
- Start the training application and go to the app home page
- Click on the link 'Ex 24 Delete Invalid'. Verify you receive an alert with the message "Error in request: status code: 404"
- 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."