RRExceptionHandler.java 1.7 KB

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