CostExceptionHandler.java 3.4 KB

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