package com.tuoheng.common;


import com.tuoheng.until.JsonResult;
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;

@ControllerAdvice  //控制器增强类
@Slf4j
public class ExceptionCatch {

    /**
     * 处理不可控异常1
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public JsonResult<Object> exception(Exception e){
        log.error("系统异常，异常信息：{}", e.getMessage());
        return JsonResult.error(e.getMessage());
    }

    /**
     * 处理可控异常  自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    public JsonResult<Object> exception(ServiceException e){
        log.error("catch exception:{}",e.getMessage());
        return JsonResult.error(e.getMessage());
    }
}
