package com.micer.easyball.exception;

import com.micer.easyball.pojo.ResponseResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 文件大小超限错误
 */
@ControllerAdvice
public class MyCustomMaxUploadSizeException {
    //这里注意如果是参数中有不存在的参数就会导致我们的全局异常失效
    @ExceptionHandler(value = MaxUploadSizeExceededException.class)
    @ResponseBody
    public ResponseResult MyException(HttpServletRequest request,
                                      HttpServletResponse response,
                                      MaxUploadSizeExceededException e
    )throws IOException {
        return new ResponseResult(510, "文件大小超过10MB");
    }

    @ExceptionHandler(value = AuthenticationException.class)
    @ResponseBody
    public ResponseResult handleAuthenticationException(AuthenticationException e) {
        return new ResponseResult(401, "用户名或密码错误");
    }
}