CostExceptionHandler.java 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package com.imed.costaccount.common.exception;
  2. import com.imed.costaccount.common.util.ErrorResult;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.apache.shiro.authz.UnauthorizedException;
  5. import org.springframework.dao.DataAccessException;
  6. import org.springframework.validation.BindException;
  7. import org.springframework.validation.ObjectError;
  8. import org.springframework.web.bind.MethodArgumentNotValidException;
  9. import org.springframework.web.bind.annotation.ExceptionHandler;
  10. import org.springframework.web.bind.annotation.ResponseBody;
  11. import org.springframework.web.bind.annotation.RestControllerAdvice;
  12. import java.io.UnsupportedEncodingException;
  13. import java.util.List;
  14. @Slf4j
  15. @RestControllerAdvice
  16. public class CostExceptionHandler {
  17. @ExceptionHandler(value = CostException.class)
  18. public ErrorResult handlerEmosException(CostException e) {
  19. e.printStackTrace();
  20. log.info("GlobalExceptionHandler...");
  21. log.info("错误代码:" + e.getMessage());
  22. return ErrorResult.errorMsg(500, e.getMessage());
  23. }
  24. // @ResponseStatus(value = HttpStatus.BAD_REQUEST)
  25. @ExceptionHandler(value = MethodArgumentNotValidException.class)
  26. public ErrorResult handlerMethodArgumentNotValidException(MethodArgumentNotValidException e) {
  27. e.printStackTrace();
  28. log.info("GlobalExceptionHandler...");
  29. log.info("错误代码:" + e.getMessage());
  30. return ErrorResult.errorMsg(500, this.formatAllErrorMessages(e.getBindingResult().getAllErrors()));
  31. }
  32. @ExceptionHandler(value = BindException.class)
  33. public ErrorResult handlerBindException(BindException e) {
  34. e.printStackTrace();
  35. log.info("GlobalExceptionHandler...");
  36. log.info("错误代码:" + e.getMessage());
  37. return ErrorResult.errorMsg(500, this.formatAllErrorMessages(e.getBindingResult().getAllErrors()));
  38. }
  39. // @ResponseStatus(value = HttpStatus.UNAUTHORIZED)
  40. @ExceptionHandler(value = UnauthorizedException.class)
  41. public ErrorResult handlerUnauthorizedException(UnauthorizedException e) {
  42. e.printStackTrace();
  43. log.info("GlobalExceptionHandler...");
  44. log.info("错误代码:" + e.getMessage());
  45. return ErrorResult.errorMsg(403,"您的权限不足,请联系管理员添加");
  46. }
  47. // @ResponseStatus(value = HttpStatus.BAD_REQUEST)
  48. @ExceptionHandler(value = Exception.class)
  49. public ErrorResult handlerException(Exception e) {
  50. e.printStackTrace();
  51. log.info("===============================GlobalExceptionHandler异常信息===============================");
  52. log.info("错误代码:" + e.getLocalizedMessage());
  53. return ErrorResult.errorMsg(500,"预期之外错误,请联系管理员~");
  54. }
  55. private String formatAllErrorMessages(List<ObjectError> errors) {
  56. StringBuffer errorMsg = new StringBuffer();
  57. errors.forEach(error -> {
  58. String defaultMessage = error.getDefaultMessage();
  59. try {
  60. defaultMessage = new String(defaultMessage.getBytes("UTF-8"), "UTF-8");
  61. } catch (UnsupportedEncodingException e) {
  62. e.printStackTrace();
  63. }
  64. errorMsg.append(defaultMessage).append(';');
  65. });
  66. return errorMsg.toString();
  67. }
  68. @ExceptionHandler(DataAccessException.class)
  69. @ResponseBody
  70. public ErrorResult handleDataAccessException(DataAccessException e) {
  71. log.error("数据库错误:{}", e.getMessage(), e);
  72. return ErrorResult.errorMsg(500,"数据库错误");
  73. }
  74. }