Skip to content

Commit

Permalink
Add Error Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
edysegura committed May 19, 2020
1 parent 6e10fb8 commit fa75f6d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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<GenericErrorResponse> handleException(Throwable e) {
GenericErrorResponse error = new GenericErrorResponse(HttpStatus.BAD_REQUEST, e.getMessage());
return new ResponseEntity<GenericErrorResponse>(error, HttpStatus.BAD_REQUEST);
}

}

0 comments on commit fa75f6d

Please sign in to comment.