CostExceptionHandler.java 3.0 KB

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