# 全局异常处理

# 404

页面:

<!doctype html>
<html lang="en" xmlns:sa="http://www.thymeleaf.org/extras/sa-token" xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <title>
        我的预约
    </title>
    <!-- Required meta tags -->
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />

    <!-- Bootstrap CSS v5.2.1 -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous" />
</head>

<body style="background-color: rgb(55, 89, 161);">
    <main>
        <div class="container" style="color: aliceblue;text-align: center;margin-top: 10%;">
            <img src="http://cdn.qiniu.liyansheng.top/typora/404.png" alt="404">
            <br>
            <h2>抱歉,没有找到您请求的页面!</h2>
            <h2>Sorry,didn't find the page you requet.</h2>
            <a href="/" style="color: aliceblue;">返回首页</a>
        </div>
    </main>
    <footer>
        <!-- place footer here -->
    </footer>
    <!-- Bootstrap JavaScript Libraries -->
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.8/dist/umd/popper.min.js"
        integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r"
        crossorigin="anonymous"></script>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.min.js"
        integrity="sha384-BBtl+eGJRgqQAUMxJ7pMwbEyER4l1g+O15P+16Ep7Q9Q+zqX6gSbd85u4mG4QzX+"
        crossorigin="anonymous"></script>
</body>

</html>

后端配置:

application.yml

spring:
  mvc:
    throw-exception-if-no-handler-found: true
  resources:
    add-mappings: true

自定义错误类实现接口:

package com.lys.sams.controller;

import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class CustomErrorController implements ErrorController {

    @RequestMapping("/error")
    public String getError() {
        return "404";
    }

    @Override
    public String getErrorPath() {
        return "/error";
    }
}

# 权限不足

异常:

cn.dev33.satoken.exception.NotRoleException: 无此角色:admin
	at cn.dev33.satoken.stp.StpLogic.checkRoleAnd(StpLogic.java:1826)

页面:

<!doctype html>
<html lang="en" xmlns:sa="http://www.thymeleaf.org/extras/sa-token" xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <title>
        权限不足
    </title>
    <!-- Required meta tags -->
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />

    <!-- Bootstrap CSS v5.2.1 -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous" />
</head>

<body style="background-color: rgb(55, 89, 161);">
    <main>
        <div class="container" style="color: aliceblue;text-align: center;margin-top: 10%;">
            <img src="http://cdn.qiniu.liyansheng.top/typora/no_auc.png" alt="404">
            <br>
            <h2>抱歉,当前操作不可进行!</h2>
            <h2>Sorry, the current operation is not available.</h2>
            <a href="/" style="color: aliceblue;">我知道了,返回首页</a>
        </div>
    </main>
    <footer>
        <!-- place footer here -->
    </footer>
    <!-- Bootstrap JavaScript Libraries -->
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.8/dist/umd/popper.min.js"
        integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r"
        crossorigin="anonymous"></script>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.min.js"
        integrity="sha384-BBtl+eGJRgqQAUMxJ7pMwbEyER4l1g+O15P+16Ep7Q9Q+zqX6gSbd85u4mG4QzX+"
        crossorigin="anonymous"></script>
</body>

</html>

主动捕获异常和转发:

@RestControllerAdvice
public class GlobalExceptionHandler {
    // 全局异常拦截 
    @ExceptionHandler
    public void handlerException(Exception e, HttpServletResponse response) throws IOException {
        if (e instanceof cn.dev33.satoken.exception.NotLoginException) {
            try {
                response.sendRedirect("/login");
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        } else if (e instanceof cn.dev33.satoken.exception.NotRoleException) {
            try {
                response.sendRedirect("/notRole");
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        } else {
            response.sendRedirect("/login");
            e.printStackTrace();
        }
    }
}

TIP

其他异常,亦可同理操作