package com.lightinit.hsdatagateway.common.handler;

import com.lightinit.hsdatagateway.common.exceptions.BaseException;
import com.lightinit.hsdatagateway.modules.foreign.model.ReturnResult;
import com.lightinit.hsdatagateway.common.exceptions.BaseException;
import com.lightinit.hsdatagateway.modules.foreign.model.ReturnResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.RejectedExecutionException;

/**
 * @Author: yangrui
 * @Date: 2019/2/21 20:54
 */
@ControllerAdvice
@Slf4j
public class GlobalDefaultExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ReturnResult defaultErrorHandler(HttpServletRequest request, Exception e)  {
        log.warn(request.getRequestURL().append("  request error ").toString(),e);
        return new ReturnResult(null, "fail", "500", e.getMessage());
    }


    @ExceptionHandler(value = BaseException.class)
    @ResponseBody
    public ReturnResult baseExceptionHandler(HttpServletRequest request, BaseException e){
        log.warn(request.getRequestURL().append("  request error ").toString(),e);
        return new ReturnResult(null, "fail", e.code, e.getMessage());
    }

    @ExceptionHandler(value = RejectedExecutionException.class)
    @ResponseBody
    public ReturnResult rejectedExecutionExceptionHandler(HttpServletRequest request, BaseException e){
        log.warn(request.getRequestURL().append(" thread pool full  error ").toString(),e);
        return new ReturnResult(null, "fail", "500", "服务器繁忙，请稍后再试");
    }

}
