package com.upload.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

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

/**
 * <p>
 * 全局异常捕获处理
 * </p>
 */
@Slf4j
@RestControllerAdvice
public class GlobalDefaultExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public RestResponse<?> exception(final HttpServletRequest request, final HttpServletResponse response, final Exception error) {
        log.error(ValuePool.LOG_GLOBAL_EXCEPTION_LINE);
        log.error("{}{} {}", ValuePool.LOG_URL, request.getMethod(), request.getRequestURI());
        log.error(ValuePool.LOG_STATUS + response.getStatus());
        log.error(ValuePool.LOG_EXCEPTION, error);

        if (error instanceof IllegalArgumentException) {
            return RestResponse.failParam();
        }
        return RestResponse.exception();
    }
}
