package com.rviewer.skeletons.application.exception; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; @ControllerAdvice public class ControllerExceptionHandler { public static final String INVALID_BODY = "Invalid body provided, check the payload."; @ExceptionHandler(CartNotFoundException.class) public ResponseEntity handleCartNotFound(CartNotFoundException e) { return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND); } @ExceptionHandler(CartAlreadyExistsException.class) public ResponseEntity handleCartAlreadyExists(CartAlreadyExistsException e) { return new ResponseEntity<>(e.getMessage(), HttpStatus.CONFLICT); } @ExceptionHandler({MethodArgumentNotValidException.class, HttpMessageNotReadableException.class}) @ResponseStatus(HttpStatus.BAD_REQUEST) public ResponseEntity handleInvalidBody() { return new ResponseEntity<>(INVALID_BODY, HttpStatus.BAD_REQUEST); } }