package com.sky.ioc.config;

import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.sky.ioc.constant.Constant;
import com.sky.ioc.tool.ReturnMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * @author LunCe
 *  异常类   （将异常信息返回）
 *
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(TokenExpiredException.class)
    public ReturnMsg tokenExpiredException(Exception e) {
        log.error(e.getMessage());
        return ReturnMsg.fail(Constant.MSG_CODE.LOGIN_TIMEOUT, "登录失效");
    }

    @ExceptionHandler(JWTVerificationException.class)
    public ReturnMsg JWTVerificationException(Exception e) {
        log.error(e.getMessage());
        return ReturnMsg.fail(Constant.MSG_CODE.SERVER_ERROR, "请登录后重试");
    }

    @ExceptionHandler(Exception.class)
    public ReturnMsg exception(Exception e) {
        log.error(e.getMessage());
        return ReturnMsg.fail(e.getMessage());
    }



//    public static String getExceptionAllinformation_01(Exception ex) {
//        ByteArrayOutputStream out = new ByteArrayOutputStream();
//        PrintStream pout = new PrintStream(out);
//        ex.printStackTrace(pout);
//        String ret = new String(out.toByteArray());
//        pout.close();
//        try {
//            out.close();
//        } catch (Exception e) {
//        }
//        return ret;
//    }

//    public String getExceptionMessage(Exception ex) {
//        StringWriter stringWriter= new StringWriter();
//        PrintWriter writer= new PrintWriter(stringWriter);
//        ex.printStackTrace(writer);
//        StringBuffer buffer= stringWriter.getBuffer();
//        return buffer.toString();
//    }

//    @ExceptionHandler(IndexOutOfBoundsException.class)
//    public String arrayOutOfRangeException(Exception e) {
//        LOGGER.error(e.getMessage(), e);
//        return "数组越界 "  + e.getMessage();
//    }
//
//    @ExceptionHandler(RuntimeException.class)
//    public String handleRuntimeException(RuntimeException e, HttpServletRequest request) {
//        String requestURI = request.getRequestURI();
//        LOGGER.error("请求地址'{}',发生未知异常.", requestURI, e);
//        return "请求地址'{"+requestURI+"}'，发生异常为：'{"+e.getMessage()+"'}" + e;
//    }
}

