package com.gyee.runeconomy.dto.FiveLoss.exception;

import com.alibaba.fastjson.JSONObject;
import com.gyee.runeconomy.dto.result.JsonResult;
import com.gyee.runeconomy.dto.result.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class AdviceException {

    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(Exception.class)
    public JSONObject runException(Exception e)
    {
        log.error(e.getMessage());
        return JsonResult.error(ResultCode.ERROR);
    }


    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(NullPointerException.class)
    public JSONObject nullPointerException(NullPointerException e)
    {
        log.error(e.getMessage());
        return JsonResult.error(ResultCode.ERROR);
    }


    /**
     * 拦截自定义异常
     */
    @ExceptionHandler(RuntimeException.class)
    public JSONObject runException(RuntimeException e)
    {
        log.error(e.getMessage());
        return JsonResult.error(ResultCode.ERROR);
    }



    /**
     * 拦截自定义异常
     */
    @ExceptionHandler(CustomException.class)
    public JSONObject customException(CustomException e)
    {
        log.error(e.getMessage());
        return JsonResult.error(e.getCode(), e.getMessage());
    }

}
