RRExceptionHandler.java 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package com.kmall.common.utils;
  2. import org.apache.shiro.authz.AuthorizationException;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.dao.DuplicateKeyException;
  6. import org.springframework.web.bind.annotation.ExceptionHandler;
  7. import org.springframework.web.bind.annotation.RestControllerAdvice;
  8. import java.util.HashMap;
  9. /**
  10. * 异常处理器
  11. *
  12. * @author Scott
  13. * @email
  14. * @date 2016年10月27日 下午10:16:19
  15. */
  16. @RestControllerAdvice
  17. public class RRExceptionHandler {
  18. private Logger logger = LoggerFactory.getLogger(getClass());
  19. /**
  20. * 自定义异常
  21. */
  22. @ExceptionHandler(RRException.class)
  23. public R handleRRException(RRException e) {
  24. R r = new R();
  25. r.put("code", e.getCode());
  26. r.put("msg", e.getMessage());
  27. return r;
  28. }
  29. @ExceptionHandler(DuplicateKeyException.class)
  30. public R handleDuplicateKeyException(DuplicateKeyException e) {
  31. logger.error(e.getMessage(), e);
  32. return R.error("数据库中已存在该记录");
  33. }
  34. @ExceptionHandler(AuthorizationException.class)
  35. public R handleAuthorizationException(AuthorizationException e) {
  36. logger.error(e.getMessage(), e);
  37. return R.error("没有权限,请联系管理员授权");
  38. }
  39. @ExceptionHandler(Exception.class)
  40. public R handleException(Exception e) {
  41. logger.error(e.getMessage(), e);
  42. return R.error();
  43. }
  44. @ExceptionHandler(ApiRRException.class)
  45. public Object handleApiRRException(ApiRRException e) {
  46. HashMap result = new HashMap();
  47. result.put("errno", e.getErrno());
  48. result.put("errmsg", e.getErrmsg());
  49. return result;
  50. }
  51. }