package com.liang.common.advice;

import com.liang.common.base.BaseResult;
import com.liang.common.config.ResponseInfo;
import com.liang.common.enums.ResultCode;
import com.liang.common.exception.CustomDivException;
import com.liang.common.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.UnsatisfiedServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import javax.validation.ConstraintViolationException;

/**
 * springmvc异常处理
 *
 * @author wjw修订
 */

@RestControllerAdvice
@Slf4j
public class ExceptionHandlerAdvice {
    @ExceptionHandler({CustomException.class})
    @ResponseBody
    public BaseResult customException(RuntimeException exception) {
        return BaseResult.failure(ResultCode.FAIL, exception.getMessage());
    }

    @ExceptionHandler({CustomDivException.class})
    @ResponseBody
    public String customDivException(RuntimeException exception) {
        return " <h1 class='grid-demo layui-bg-red'>" + exception.getMessage() +
                "</h1>";
    }

    @ExceptionHandler({IllegalArgumentException.class})
    @ResponseBody
    public BaseResult parameterException(RuntimeException exception) {
        return BaseResult.failure(ResultCode.ILLEGALARGUMENT);
    }
}
