package com.xmomen.module.core.web.exception;

import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.servlet.ModelAndView;


@ControllerAdvice
public class DefaultExceptionHandler {

    // 处理未授权异常
    @ExceptionHandler({UnauthorizedException.class})
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public ModelAndView processUnauthenticatedException(NativeWebRequest request, UnauthorizedException e) {
        // 创建一个ModelAndView对象
        ModelAndView mv = new ModelAndView();
        // 将异常对象添加到ModelAndView中
        mv.addObject("exception", e);
        // 设置视图名为"unauthorized"
        mv.setViewName("unauthorized");
        // 返回ModelAndView对象
        return mv;
    }


    // 处理500内部服务器错误
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ModelAndView internalServerError500Exception(NativeWebRequest request, UnauthorizedException e) {
        // 创建一个ModelAndView对象
        ModelAndView mv = new ModelAndView();
        // 将异常对象添加到ModelAndView中
        mv.addObject("exception", e);
        // 设置视图名为"unauthorized"
        mv.setViewName("unauthorized");
        // 返回ModelAndView对象
        return mv;
    }

}
