package com.nokia.hb.config.web;

import com.nokia.common.R;
import com.nokia.common.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingRequestValueException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ValidationException;

/**
 * 请求异常处理
 */
@Slf4j
@RestControllerAdvice
public class ControllerExceptionHandler
{
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResponseEntity<Object> httpRequestMethodNotSupportedExceptionHandler(HttpRequestMethodNotSupportedException e)
    {
        log.warn(e.getMessage());
        return ResponseEntity.ok().body(R.error().message(e.getMessage()));
    }

    @ExceptionHandler(MissingRequestValueException.class)
    public ResponseEntity<Object> missingRequestValueExceptionHandler(MissingRequestValueException e)
    {
        log.warn(e.getMessage());
        return ResponseEntity.ok().body(R.error().message(e.getMessage()));
    }

    @ExceptionHandler(HttpMessageConversionException.class)
    public ResponseEntity<Object> httpMessageConversionExceptionHandler(HttpMessageConversionException e)
    {
        log.warn(e.getMessage());
        return ResponseEntity.ok().body(R.error().message(e.getMessage()));
    }

    @ExceptionHandler(TypeMismatchException.class)
    public ResponseEntity<Object> typeMismatchExceptionHandler(TypeMismatchException e)
    {
        log.warn(e.getMessage());
        return ResponseEntity.ok().body(R.error().message(e.getMessage()));
    }

    @ExceptionHandler(BindException.class)
    public ResponseEntity<Object> bindExceptionHandler(BindException e)
    {
        FieldError fieldError = e.getBindingResult().getFieldError();
        String message = "";
        if (fieldError != null)
        {
            message = fieldError.getDefaultMessage();
        }
        log.warn(message);
        return ResponseEntity.ok().body(R.error().message(message));
    }

    @ExceptionHandler(ValidationException.class)
    public ResponseEntity<Object> validationExceptionHandler(ValidationException e)
    {
        log.warn(e.getMessage());
        return ResponseEntity.ok().body(R.error().message(e.getMessage()));
    }

    @ExceptionHandler(BizException.class)
    public ResponseEntity<Object> bizExceptionHandler(BizException e)
    {
        return ResponseEntity.ok().body(R.error(e.getMessage()));
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<Object> exceptionHandler(Exception e)
    {
        log.error("╭( ′• o •′ )╭☞ 发生错误了 {}", e.getMessage(), e);
        return ResponseEntity.ok().body(R.error());
    }
}
