美文网首页
GlobalExceptionHandle(全局统一异常处理)

GlobalExceptionHandle(全局统一异常处理)

作者: knock | 来源:发表于2020-07-25 16:44 被阅读0次

GlobalExceptionHandle.java

package kr.weitao.starter.config;

import kr.weitao.common.exception.CommonException;
import kr.weitao.common.exception.ServiceException;
import kr.weitao.starter.model.DataResponse;
import kr.weitao.starter.model.Status;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.List;

/**
 * 全局统一异常处理
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandle {

    /**
     * 业务异常
     */
    @ExceptionHandler(value = ServiceException.class)
    public ResponseEntity<DataResponse> handle(ServiceException e) {
        e.printStackTrace();
        DataResponse failure = new DataResponse().setStatus(Status.FAILED).setMsg(e.getMsg());
        return ResponseEntity.ok().body(failure);
    }

    @ExceptionHandler(value = CommonException.class)
    public ResponseEntity<DataResponse> handle(CommonException e) {
        e.printStackTrace();
        DataResponse failure = new DataResponse().setStatus(Status.FAILED).setMsg(e.getMessage());
        return ResponseEntity.ok().body(failure);
    }

    /**
     * 400错误
     */
    @ExceptionHandler({HttpMessageNotReadableException.class})
    public ResponseEntity<DataResponse> requestNotReadable(HttpMessageNotReadableException ex) {
        ex.printStackTrace();
        DataResponse failure = new DataResponse().setStatus(Status.FAILED).setMsg(ex.getMessage());
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(failure);
    }

    /**
     * 400错误
     * @param ex
     * @return
     */
    @ExceptionHandler({TypeMismatchException.class})
    public ResponseEntity<DataResponse> requestTypeMismatch(TypeMismatchException ex) {
        ex.printStackTrace();
        DataResponse failure = new DataResponse().setStatus(Status.FAILED).setMsg(ex.getMessage());
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(failure);
    }

    /**
     * 400错误
     *
     * @param ex
     * @return
     */
    @ExceptionHandler({MissingServletRequestParameterException.class})
    public ResponseEntity<DataResponse> requestMissingServletRequest(MissingServletRequestParameterException ex) {
        ex.printStackTrace();
        DataResponse failure = new DataResponse().setStatus(Status.FAILED).setMsg(ex.getMessage());
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(failure);
    }

    /**
     * IO异常
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(IOException.class)
    public ResponseEntity<DataResponse> iOExceptionHandler(IOException ex) {
        ex.printStackTrace();
        DataResponse failure = new DataResponse().setStatus(Status.FAILED).setMsg(ex.getMessage());
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(failure);
    }

    /**
     * 405错误
     *
     * @param ex
     * @return
     */
    @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
    public ResponseEntity<DataResponse> request405(HttpRequestMethodNotSupportedException ex) {
        ex.printStackTrace();
        DataResponse failure = new DataResponse().setStatus(Status.FAILED).setMsg("不支持请求方法");
        return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED).body(failure);
    }


    /**
     * 超时异常
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(SocketTimeoutException.class)
    public ResponseEntity<DataResponse> SocketTimeoutException(SocketTimeoutException ex) {
        ex.printStackTrace();
        DataResponse failure = new DataResponse().setStatus(Status.FAILED).setMsg("连接超时,请检查网络环境或重试");
        return ResponseEntity.status(HttpStatus.REQUEST_TIMEOUT).body(failure);
    }

    /**
     * 处理入参异常
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ResponseEntity<DataResponse> handleIllegalParamException(MethodArgumentNotValidException e) {
        e.printStackTrace();
        String message = "参数不合法";
        List<FieldError> errors = e.getBindingResult().getFieldErrors();
        if (errors.size() > 0) {
            message = errors.get(0).getDefaultMessage();
        }
        DataResponse failure = new DataResponse().setStatus(Status.FAILED).setMsg(message);
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(failure);
    }

    /**
     * 其他类型的异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = RuntimeException.class)
    public ResponseEntity<DataResponse> handle(Exception e) {
        e.printStackTrace();
        DataResponse failure = new DataResponse().setStatus(Status.FAILED).setMsg(e.getMessage());
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(failure);
    }


}


ServiceException.java

package kr.weitao.common.exception;

import lombok.Data;

/**
 * 统一异常处理类
 */
@Data
public class ServiceException extends RuntimeException {
    private static final long serialVersionUID = -6865985823284407231L;

    private String msg;

    public ServiceException(String msg) {
        super(msg);
        this.msg = msg;
    }


}

相关文章

网友评论

      本文标题:GlobalExceptionHandle(全局统一异常处理)

      本文链接:https://www.haomeiwen.com/subject/vtrmlktx.html