package com.chinacreator.video.exception;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import com.chinacreator.video.dto.DataOutPipe;


@ControllerAdvice
public class GlobalExceptionHandler {
	
	private static Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);
	private static Logger ErrorLOGGER = LoggerFactory.getLogger("ERROR");

	@ExceptionHandler(value = Exception.class)
	@ResponseBody
    public DataOutPipe<?> defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
	    ErrorLOGGER.error("", e);  
	 	e.printStackTrace();
	 	DataOutPipe<?> r = new DataOutPipe<>();
	 	r.setErrorinfo("系统忙，请稍后再试，"+e.getMessage());
        r.setResultcode("8000");
        return r;
    }

    @ExceptionHandler(value = BussinessException.class)
    @ResponseBody
    public DataOutPipe<?> jsonErrorHandler(HttpServletRequest req, BussinessException e) throws Exception {
    	LOGGER.error("",e);  
        DataOutPipe<?> r = new DataOutPipe<>();
        r.setErrorinfo(e.getMessage());
        r.setResultcode(e.getCode());
        return r;
    }
	
}
