package com.rongwei.bscommon.sys.handler;

import com.rongwei.rwcommon.base.R;
import com.rongwei.rwcommon.base.exception.CustomException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Order(1)
@RestControllerAdvice
public class ControllerExceptionHandler {
    private final Logger log = LoggerFactory.getLogger(this.getClass().getName());

    @ExceptionHandler(RuntimeException.class)
    public R handlerException(RuntimeException ex) {
        log.error("发生业务异常!原因是：{}", ex);
        return R.error(ex.getMessage());
    }

    @ExceptionHandler(CustomException.class)
    public R handlerException(CustomException ex) {
        log.error("发生业务异常!原因是：{}", ex);
        return R.error(ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public R handlerUnknownException(Exception ex) {
        log.error("发生未知异常!原因是: {}", ex);
        return R.error(ex.getMessage());
    }
}
