亿众分发系统
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

RRExceptionHandler.java 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * Copyright (c) 2016-2019 人人开源 All rights reserved.
  3. *
  4. * https://www.renren.io
  5. *
  6. * 版权所有,侵权必究!
  7. */
  8. package io.renren.exception;
  9. import io.renren.common.exception.RRException;
  10. import io.renren.common.utils.R;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.dao.DuplicateKeyException;
  14. import org.springframework.web.bind.annotation.ExceptionHandler;
  15. import org.springframework.web.bind.annotation.RestControllerAdvice;
  16. /**
  17. * 异常处理器
  18. *
  19. * @author Mark sunlightcs@gmail.com
  20. */
  21. @RestControllerAdvice
  22. public class RRExceptionHandler {
  23. private Logger logger = LoggerFactory.getLogger(getClass());
  24. /**
  25. * 处理自定义异常
  26. */
  27. @ExceptionHandler(RRException.class)
  28. public R handleRRException(RRException e){
  29. R r = new R();
  30. r.put("code", e.getCode());
  31. r.put("msg", e.getMessage());
  32. return r;
  33. }
  34. @ExceptionHandler(DuplicateKeyException.class)
  35. public R handleDuplicateKeyException(DuplicateKeyException 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. }