Review Not Allowed Exception

In our restaurant review platform, we need to prevent users from reviewing the same restaurant multiple times, or attempting to edit theor review after the 24 hour window.

We'll implement this by creating a custom exception and adding appropriate handling in our error controller.

Creating the Exception

Let's create our ReviewNotAllowedException class:

package com.devtiro.restaurant.exceptions; public class ReviewNotAllowedException extends BaseException { public ReviewNotAllowedException() { super(); } public ReviewNotAllowedException(String message) { super(message); } public ReviewNotAllowedException(String message, Throwable cause) { super(message, cause); } }

By extending BaseException, our new exception inherits common functionality while allowing us to handle it specifically when needed.

Exception Handling

We'll add a dedicated handler method to our ErrorController to manage this exception:

@ExceptionHandler(ReviewNotAllowedException.class) public ResponseEntity<ErrorDto> handleRestaurantReviewNotAllowedException( ReviewNotAllowedException ex) { // Log the exception for debugging log.error("Caught ReviewNotAllowedException exception", ex); // Create a user-friendly error response ErrorDto error = ErrorDto.builder() .status(HttpStatus.BAD_REQUEST.value()) .message("The specified review cannot be created or updated") .build(); return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST); }

This handler transforms our exception into a clear HTTP 400 BAD REQUEST response.

Summary

  • Created ReviewNotAllowedException to handle duplicate review attempts
  • Added exception handler in ErrorController returning HTTP 400 responses
  • Exception extends BaseException for consistent error handling
© 2026 Devtiro Ltd. All rights reserved