diff --git a/infrastructure/in/rest-api/src/main/java/com/techivw/webprice/infrastructure/in/controllers/handlers/AdapterExceptionHandler.java b/infrastructure/in/rest-api/src/main/java/com/techivw/webprice/infrastructure/in/controllers/handlers/AdapterExceptionHandler.java index 50b1daa..9cf0ec0 100644 --- a/infrastructure/in/rest-api/src/main/java/com/techivw/webprice/infrastructure/in/controllers/handlers/AdapterExceptionHandler.java +++ b/infrastructure/in/rest-api/src/main/java/com/techivw/webprice/infrastructure/in/controllers/handlers/AdapterExceptionHandler.java @@ -2,13 +2,16 @@ package com.techivw.webprice.infrastructure.in.controllers.handlers; import com.techivw.webprice.application.exceptions.NotFoundException; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; import java.util.Date; import java.util.HashMap; import java.util.Map; +import static org.springframework.http.HttpStatus.BAD_REQUEST; import static org.springframework.http.HttpStatus.NOT_FOUND; @RestControllerAdvice @@ -24,4 +27,27 @@ public class AdapterExceptionHandler { return new ResponseEntity<>(body, NOT_FOUND); } + + @ExceptionHandler(MissingServletRequestParameterException.class) + public ResponseEntity> handleMissingParamException(MissingServletRequestParameterException ex) { + Map body = new HashMap<>(); + body.put("timestamp", new Date()); + body.put("status", BAD_REQUEST.value()); + body.put("error", "Bad Request"); + body.put("message", "Required parameter '" + ex.getParameterName() + "' is missing"); + + return new ResponseEntity<>(body, BAD_REQUEST); + } + + @ExceptionHandler(MethodArgumentTypeMismatchException.class) + public ResponseEntity> handleTypeMismatchException(MethodArgumentTypeMismatchException ex) { + Map body = new HashMap<>(); + body.put("timestamp", new Date()); + body.put("status", BAD_REQUEST.value()); + body.put("error", "Bad Request"); + body.put("message", "Parameter '" + ex.getName() + "' has invalid format."); + + return new ResponseEntity<>(body, BAD_REQUEST); + } + }