-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
src/main/java/br/com/spring/restful/errorhandler/GenericErrorResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/br/com/spring/restful/errorhandler/RestExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |