package com.ruoyi.file.config;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.file.object.UploadException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 外部文件上传异常处理器
 * 
 * @author ruoyi
 */
@RestControllerAdvice
@Order(Integer.MIN_VALUE)
public class UploadExceptionHandler
{
    private static final Logger log = LoggerFactory.getLogger(UploadExceptionHandler.class);

    /**
     * 拦截文件上传异常
     */
    @ExceptionHandler(UploadException.class)
    public AjaxResult handleUploadException(UploadException e, HttpServletRequest request, HttpServletResponse response)
    {
        String requestURI = request.getRequestURI();
        log.error("外部文件上传异常: 请求地址'{}',发生异常 {} = '{}'.", requestURI, e.code, e.getMessage());
        response.setStatus(e.code);
        return AjaxResult.error(e.getMessage());
    }

}
