From 4092258e6986a7f35a73c45e89fa4873ed59acf4 Mon Sep 17 00:00:00 2001 From: linfeng <2445465217@qq.com> Date: Mon, 17 Apr 2023 09:44:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/exception/LinfengException.java | 63 ++++++++++++++++++ .../exception/LinfengExceptionHandler.java | 66 +++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 src/main/java/io/linfeng/common/exception/LinfengException.java create mode 100644 src/main/java/io/linfeng/common/exception/LinfengExceptionHandler.java diff --git a/src/main/java/io/linfeng/common/exception/LinfengException.java b/src/main/java/io/linfeng/common/exception/LinfengException.java new file mode 100644 index 0000000..acfcd57 --- /dev/null +++ b/src/main/java/io/linfeng/common/exception/LinfengException.java @@ -0,0 +1,63 @@ +/** + * ----------------------------------- + * 林风社交论坛开源版本请务必保留此注释头信息 + * 开源地址: https://gitee.com/virus010101/linfeng-community + * 商业版演示站点: https://www.linfeng.tech + * 商业版购买联系技术客服 QQ: 3582996245 + * 可正常分享和学习源码,不得转卖或非法牟利! + * Copyright (c) 2021-2023 linfeng all rights reserved. + * 版权所有 ,侵权必究! + * ----------------------------------- + */ +package io.linfeng.common.exception; + +/** + * 自定义异常 + * + */ +public class LinfengException extends RuntimeException { + private static final long serialVersionUID = 1L; + + private String msg; + private int code = 500; + + public LinfengException(String msg) { + super(msg); + this.msg = msg; + } + + public LinfengException(String msg, Throwable e) { + super(msg, e); + this.msg = msg; + } + + public LinfengException(String msg, int code) { + super(msg); + this.msg = msg; + this.code = code; + } + + public LinfengException(String msg, int code, Throwable e) { + super(msg, e); + this.msg = msg; + this.code = code; + } + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + +} diff --git a/src/main/java/io/linfeng/common/exception/LinfengExceptionHandler.java b/src/main/java/io/linfeng/common/exception/LinfengExceptionHandler.java new file mode 100644 index 0000000..013b0d5 --- /dev/null +++ b/src/main/java/io/linfeng/common/exception/LinfengExceptionHandler.java @@ -0,0 +1,66 @@ +/** + * ----------------------------------- + * 林风社交论坛开源版本请务必保留此注释头信息 + * 开源地址: https://gitee.com/virus010101/linfeng-community + * 商业版演示站点: https://www.linfeng.tech + * 商业版购买联系技术客服 QQ: 3582996245 + * 可正常分享和学习源码,不得转卖或非法牟利! + * Copyright (c) 2021-2023 linfeng all rights reserved. + * 版权所有 ,侵权必究! + * ----------------------------------- + */ +package io.linfeng.common.exception; + +import io.linfeng.common.utils.R; +import org.apache.shiro.authz.AuthorizationException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.dao.DuplicateKeyException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.servlet.NoHandlerFoundException; + +/** + * 异常处理器 + * + */ +@RestControllerAdvice +public class LinfengExceptionHandler { + private Logger logger = LoggerFactory.getLogger(getClass()); + + /** + * 处理自定义异常 + */ + @ExceptionHandler(LinfengException.class) + public R handleException(LinfengException e){ + R r = new R(); + r.put("code", e.getCode()); + r.put("msg", e.getMessage()); + + return r; + } + + @ExceptionHandler(NoHandlerFoundException.class) + public R handlerNoFoundException(Exception e) { + logger.error(e.getMessage(), e); + return R.error(404, "路径不存在请查看路径是否正确"); + } + + @ExceptionHandler(DuplicateKeyException.class) + public R handleDuplicateKeyException(DuplicateKeyException e){ + logger.error(e.getMessage(), e); + return R.error("数据库中已存在该记录"); + } + + @ExceptionHandler(AuthorizationException.class) + public R handleAuthorizationException(AuthorizationException e){ + logger.error(e.getMessage(), e); + return R.error("你没有该操作权限哦"); + } + + @ExceptionHandler(Exception.class) + public R handleException(Exception e){ + logger.error(e.getMessage(), e); + return R.error(); + } +}