Files
cartchain/src/main/java/com/rviewer/skeletons/application/exception/ControllerExceptionHandler.java
2022-10-08 12:27:21 +02:00

33 lines
1.3 KiB
Java

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<Object> handleCartNotFound(CartNotFoundException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
}
@ExceptionHandler(CartAlreadyExistsException.class)
public ResponseEntity<Object> handleCartAlreadyExists(CartAlreadyExistsException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.CONFLICT);
}
@ExceptionHandler({MethodArgumentNotValidException.class, HttpMessageNotReadableException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<Object> handleInvalidBody() {
return new ResponseEntity<>(INVALID_BODY, HttpStatus.BAD_REQUEST);
}
}