Skip to content

Commit

Permalink
Handle the RemovalNotAllowedException in the AbstractWebController as…
Browse files Browse the repository at this point in the history
… an UNAUTHORIZED
  • Loading branch information
openwms committed Apr 8, 2024
1 parent 04d9eae commit 33626a3
Showing 1 changed file with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.ameba.http.AbstractBase;
import org.ameba.http.Response;
import org.openwms.core.exception.ExceptionCodes;
import org.openwms.core.listener.RemovalNotAllowedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -38,9 +39,7 @@
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import java.net.URI;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

/**
* A AbstractWebController.
Expand All @@ -55,7 +54,7 @@ public abstract class AbstractWebController {

@ExceptionHandler(BehaviorAwareException.class)
protected ResponseEntity<Response<?>> handleBehaviorAwareException(BehaviorAwareException bae) {
EXC_LOGGER.error("[P] Presentation Layer Exception: " + bae.getLocalizedMessage(), bae);
EXC_LOGGER.error("[P] Presentation Layer Exception: {}", bae.getLocalizedMessage(), bae);
return new ResponseEntity<>(Response.newBuilder()
.withMessage(bae.getMessage())
.withMessageKey(bae.getMessageKey())
Expand All @@ -66,9 +65,21 @@ protected ResponseEntity<Response<?>> handleBehaviorAwareException(BehaviorAware
);
}

@ExceptionHandler(RemovalNotAllowedException.class)
protected ResponseEntity<Response<?>> handleRemovalNotAllowedException(RemovalNotAllowedException rnae) {
EXC_LOGGER.error("[P] Presentation Layer Exception: {}", rnae.getLocalizedMessage(), rnae);
return new ResponseEntity<>(Response.newBuilder()
.withMessage(rnae.getMessage())
.withMessageKey(rnae.getMessageKey())
.withObj(rnae.getData())
.build(),
HttpStatus.UNAUTHORIZED
);
}

@ExceptionHandler(BusinessRuntimeException.class)
protected ResponseEntity<Response<?>> handleBusinessRuntimeException(BusinessRuntimeException bre) {
EXC_LOGGER.error("[P] Presentation Layer Exception: " + bre.getLocalizedMessage(), bre);
EXC_LOGGER.error("[P] Presentation Layer Exception: {}", bre.getLocalizedMessage(), bre);
ResponseStatus annotation = bre.getClass().getAnnotation(ResponseStatus.class);
HttpStatus status = annotation != null ? annotation.value() : HttpStatus.INTERNAL_SERVER_ERROR;
return new ResponseEntity<>(Response.newBuilder()
Expand All @@ -83,7 +94,7 @@ protected ResponseEntity<Response<?>> handleBusinessRuntimeException(BusinessRun

@ExceptionHandler(HttpBusinessException.class)
protected ResponseEntity<Response<?>> handleHttpBusinessException(HttpBusinessException hbe) {
EXC_LOGGER.error("[P] Presentation Layer Exception: " + hbe.getLocalizedMessage(), hbe);
EXC_LOGGER.error("[P] Presentation Layer Exception: {}", hbe.getLocalizedMessage(), hbe);
return new ResponseEntity<>(Response.newBuilder()
.withMessage(hbe.getMessage())
.withHttpStatus(String.valueOf(hbe.getHttpStatus().value()))
Expand Down Expand Up @@ -115,7 +126,7 @@ protected ResponseEntity<Response<?>> handleTechnicalRuntimeException(TechnicalR
}

@ExceptionHandler({IllegalArgumentException.class})
public ResponseEntity<Response<?>> IllegalArgumentException(IllegalArgumentException ex) {
public ResponseEntity<Response<?>> illegalArgumentException(IllegalArgumentException ex) {
return new ResponseEntity<>(
Response.newBuilder()
.withMessage(ex.getMessage())
Expand All @@ -140,7 +151,7 @@ protected ResponseEntity<Response<?>> handleValidationException() {

@ExceptionHandler(ConstraintViolationException.class)
protected ResponseEntity<Response<?>> handleConstraintViolationException(ConstraintViolationException ex) {
List<String> properties = ex.getConstraintViolations().stream().map(c -> c.getPropertyPath().toString()).collect(Collectors.toList());
var properties = ex.getConstraintViolations().stream().map(c -> c.getPropertyPath().toString()).toList();
return new ResponseEntity<>(
Response.newBuilder()
.withMessage(translate(ExceptionCodes.VALIDATION_ERROR, properties))
Expand All @@ -153,7 +164,7 @@ protected ResponseEntity<Response<?>> handleConstraintViolationException(Constra

@ExceptionHandler(Exception.class)
protected ResponseEntity<Response<?>> handleException(Exception ex) {
EXC_LOGGER.error("[P] Presentation Layer Exception: " + ex.getLocalizedMessage(), ex);
EXC_LOGGER.error("[P] Presentation Layer Exception: {}", ex.getLocalizedMessage(), ex);
return new ResponseEntity<>(Response.newBuilder()
.withMessage(ex.getMessage())
.withMessageKey(ExceptionCodes.TECHNICAL_RT_ERROR)
Expand Down

0 comments on commit 33626a3

Please sign in to comment.