package com.heketai.website.common.exception;

import com.heketai.website.common.result.BaseResult;
import com.heketai.website.common.result.HttpCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class MyExceptionHandler {

    @ExceptionHandler
    @ResponseBody
    public BaseResult errorHandler(AuthorizationException e) {
        log.error("没有通过权限验证！e={}", e.getMessage(),e);
        return BaseResult.fail(HttpCode.AUTHENTICATION_FAIL.getCode(), HttpCode.AUTHENTICATION_FAIL.getMsg());
    }

    @ExceptionHandler
    @ResponseBody
    public BaseResult errorHandler(Exception e) {
        log.error("程序内部异常！e={}", e.getMessage(),e);
        return BaseResult.fail(HttpCode.FAIL.getCode(), HttpCode.FAIL.getMsg());
    }

    @ExceptionHandler
    @ResponseBody
    public BaseResult errorHandler(BussinessException e) {
        log.error("自定义异常消息！e={}", e.getMessage(),e);
        return BaseResult.fail("10001", e.getMessage());
    }
}