package com.chuanxia.mcp.exception;

import com.chuanxia.mcp.bean.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理
 *
 * @author liyuntian
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ResponseBody
    @ExceptionHandler(ResultException.class)
    public Result handleCustomException(ResultException e) {
        e.printStackTrace();
        log.error("进入了业务异常全局异常捕获,异常原因:{}", e.getMessage());
        return Result.error(e.getCode(), e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Result exceptionCatch(Exception e) {
        e.printStackTrace();
        log.error("系统发生异常,异常原因:{}", e.getMessage());
        return Result.error(500, "系统发生异常,原因:" + e.getMessage());
    }
}

