diff --git a/src/main/java/br/com/spring/restful/errorhandler/GenericErrorResponse.java b/src/main/java/br/com/spring/restful/errorhandler/GenericErrorResponse.java new file mode 100644 index 0000000..da0eb05 --- /dev/null +++ b/src/main/java/br/com/spring/restful/errorhandler/GenericErrorResponse.java @@ -0,0 +1,41 @@ +package br.com.spring.restful.errorhandler; + +import org.springframework.http.HttpStatus; + +public class GenericErrorResponse { + + private int httpCode = 400; + private String status = ""; + private String message = ""; + + public GenericErrorResponse(HttpStatus httpStatus, String message) { + this.httpCode = httpStatus.value(); + this.status = httpStatus.getReasonPhrase(); + this.message = message; + } + + public int getHttpCode() { + return httpCode; + } + + public void setHttpCode(int httpCode) { + this.httpCode = httpCode; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + +} \ No newline at end of file diff --git a/src/main/java/br/com/spring/restful/errorhandler/RestExceptionHandler.java b/src/main/java/br/com/spring/restful/errorhandler/RestExceptionHandler.java new file mode 100644 index 0000000..d67ef72 --- /dev/null +++ b/src/main/java/br/com/spring/restful/errorhandler/RestExceptionHandler.java @@ -0,0 +1,20 @@ +package br.com.spring.restful.errorhandler; + +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; + +@ControllerAdvice +@Order(Ordered.HIGHEST_PRECEDENCE) +public class RestExceptionHandler { + + @ExceptionHandler(Throwable.class) + protected ResponseEntity handleException(Throwable e) { + GenericErrorResponse error = new GenericErrorResponse(HttpStatus.BAD_REQUEST, e.getMessage()); + return new ResponseEntity(error, HttpStatus.BAD_REQUEST); + } + +}