package com.koobietech.eas.config;

import com.koobietech.eas.common.exception.EasException;
import com.koobietech.eas.common.result.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice

public class ExceptionAdvice {

    private static final Logger logger = LoggerFactory.getLogger(ExceptionAdvice.class);

    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(Exception.class)
    public JsonResult exceptionHandler(Exception e){
        StackTraceElement[] stackTrace = e.getStackTrace();
        e.printStackTrace();
        logger.error("运行异常提醒：");
        logger.error(e.getMessage());
        logger.error("异常堆栈信息：");
        for (StackTraceElement element : stackTrace) {
            logger.error(element.toString());
        }
        logger.error("-------------");
        return JsonResult.fail(e.getMessage(), 500);
    }

    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(EasException.class)
    public JsonResult exceptionHandler(EasException e){
        StackTraceElement[] stackTrace = e.getStackTrace();
        e.printStackTrace();
        logger.error("运行异常提醒：");
        logger.error(e.getMessage());
        logger.error("异常堆栈信息：");
        for (StackTraceElement element : stackTrace) {
            logger.error(element.toString());
        }
        logger.error("-------------");
        return JsonResult.fail(e.getMessage(), e.getCode());
    }
}
