package com.chy.borrow.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author ：沈晔浩
 * @date ：Created in 2021/9/14 17:20
 * @description：全局异常捕获
 */
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
    /**
     * 处理自定义异常
     *
     */
    @ExceptionHandler(value = DefinitionException.class)
      public Result bizExceptionHandler(DefinitionException e) {
        return Result.defineError(e);
    }

    /**
     * 处理其他异常
     *
     */
    @ExceptionHandler(value = Exception.class)
      public Result exceptionHandler( Exception e) {
        e.printStackTrace();
        if (e.getClass().getName()=="org.springframework.web.servlet.NoHandlerFoundException"){
            return Result.Error_404(404,e.getMessage());
        }else{
            return Result.otherError(ErrorEnum.INTERNAL_SERVER_ERROR);
        }
    }
}
