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;

/**
 * @author lc
 */
@RestControllerAdvice
public class ExceptionAdviceConfiguration {

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

    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(Exception.class)
    public JsonResult exceptionHandler(Exception e){
        StackTraceElement[] stackTrace = e.getStackTrace();
        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();
        logger.error("||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||");
        for (StackTraceElement element : stackTrace) {
            logger.error(element.toString());
        }
        logger.error("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
        return JsonResult.fail(e.getMessage(), e.getCode());
    }
}
