66import org .springframework .core .Ordered ;
77import org .springframework .core .annotation .Order ;
88import org .springframework .dao .DataIntegrityViolationException ;
9- import org .springframework .http .HttpStatus ;
9+ import org .springframework .http .ResponseEntity ;
1010import org .springframework .http .converter .HttpMessageNotReadableException ;
1111import org .springframework .validation .BindException ;
1212import org .springframework .web .bind .annotation .ExceptionHandler ;
13- import org .springframework .web .bind .annotation .ResponseStatus ;
1413import org .springframework .web .bind .annotation .RestController ;
1514import org .springframework .web .bind .annotation .RestControllerAdvice ;
1615import org .springframework .web .method .annotation .MethodArgumentTypeMismatchException ;
@@ -43,16 +42,13 @@ public ExceptionInfoHandler(MessageSourceAccessor messageSourceAccessor) {
4342 this .messageSourceAccessor = messageSourceAccessor ;
4443 }
4544
46- // http://stackoverflow.com/a/22358422/548473
47- @ ResponseStatus (HttpStatus .UNPROCESSABLE_ENTITY )
4845 @ ExceptionHandler (NotFoundException .class )
49- public ErrorInfo notFoundError (HttpServletRequest req , NotFoundException e ) {
46+ public ResponseEntity < ErrorInfo > notFoundError (HttpServletRequest req , NotFoundException e ) {
5047 return logAndGetErrorInfo (req , e , false , DATA_NOT_FOUND );
5148 }
5249
53- @ ResponseStatus (HttpStatus .CONFLICT ) // 409
5450 @ ExceptionHandler (DataIntegrityViolationException .class )
55- public ErrorInfo conflict (HttpServletRequest req , DataIntegrityViolationException e ) {
51+ public ResponseEntity < ErrorInfo > conflict (HttpServletRequest req , DataIntegrityViolationException e ) {
5652 String rootMsg = ValidationUtil .getRootCause (e ).getMessage ();
5753 if (rootMsg != null ) {
5854 String lowerCaseMsg = rootMsg .toLowerCase ();
@@ -65,33 +61,32 @@ public ErrorInfo conflict(HttpServletRequest req, DataIntegrityViolationExceptio
6561 return logAndGetErrorInfo (req , e , true , DATA_ERROR );
6662 }
6763
68- @ ResponseStatus (value = HttpStatus .UNPROCESSABLE_ENTITY ) // 422
6964 @ ExceptionHandler (BindException .class )
70- public ErrorInfo bindValidationError (HttpServletRequest req , BindException e ) {
65+ public ResponseEntity < ErrorInfo > bindValidationError (HttpServletRequest req , BindException e ) {
7166 String [] details = e .getBindingResult ().getFieldErrors ().stream ()
7267 .map (messageSourceAccessor ::getMessage )
7368 .toArray (String []::new );
7469
7570 return logAndGetErrorInfo (req , e , false , VALIDATION_ERROR , details );
7671 }
7772
78- @ ResponseStatus (HttpStatus .UNPROCESSABLE_ENTITY ) // 422
7973 @ ExceptionHandler ({IllegalRequestDataException .class , MethodArgumentTypeMismatchException .class , HttpMessageNotReadableException .class })
80- public ErrorInfo validationError (HttpServletRequest req , Exception e ) {
74+ public ResponseEntity < ErrorInfo > validationError (HttpServletRequest req , Exception e ) {
8175 return logAndGetErrorInfo (req , e , false , VALIDATION_ERROR );
8276 }
8377
84- @ ResponseStatus (HttpStatus .INTERNAL_SERVER_ERROR )
8578 @ ExceptionHandler (Exception .class )
86- public ErrorInfo internalError (HttpServletRequest req , Exception e ) {
79+ public ResponseEntity < ErrorInfo > internalError (HttpServletRequest req , Exception e ) {
8780 return logAndGetErrorInfo (req , e , true , APP_ERROR );
8881 }
8982
9083 // https://stackoverflow.com/questions/538870/should-private-helper-methods-be-static-if-they-can-be-static
91- private ErrorInfo logAndGetErrorInfo (HttpServletRequest req , Exception e , boolean logStackTrace , ErrorType errorType , String ... details ) {
84+ private ResponseEntity < ErrorInfo > logAndGetErrorInfo (HttpServletRequest req , Exception e , boolean logStackTrace , ErrorType errorType , String ... details ) {
9285 Throwable rootCause = ValidationUtil .logAndGetRootCause (log , req , e , logStackTrace , errorType );
93- return new ErrorInfo (req .getRequestURL (), errorType ,
94- messageSourceAccessor .getMessage (errorType .getErrorCode ()),
95- details .length != 0 ? details : new String []{ValidationUtil .getMessage (rootCause )});
86+ return ResponseEntity .status (errorType .getStatus ())
87+ .body (new ErrorInfo (req .getRequestURL (), errorType ,
88+ messageSourceAccessor .getMessage (errorType .getErrorCode ()),
89+ details .length != 0 ? details : new String []{ValidationUtil .getMessage (rootCause )})
90+ );
9691 }
9792}
0 commit comments