package com.platform.exception;

/**
 * @author abnerhou
 * @date 2020/6/1 18:02
 * @desciption
 */


import com.platform.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
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 javax.servlet.http.HttpServletRequest;

/**
 * 全局异常捕获统一返回客户端
 */

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(CommonBaseException.class)
    @ResponseStatus(HttpStatus.OK)
    public Result CommonBaseExceptionHandler(HttpServletRequest request, CommonBaseException e) {
        log.error(request.getRequestURI() + ":" + e.getMsg());
        return Result.failure(StringUtils.isEmpty(e.getCode()) ? Result.CODE_FAILURE : e.getCode(), e.getMsg());
    }

}
