package com.imooc.handle;

import com.imooc.aspect.HttpAspect;
import com.imooc.entity.Result;
import com.imooc.exception.GirlException;
import com.imooc.utils.ResultUtil;
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;

/**
 * @Auther: curry
 * @Date: 2018/6/2 15:05
 * @Description:
 */
@ControllerAdvice
public class ExceptionHandle {
    private  final static Logger logger = LoggerFactory.getLogger(HttpAspect.class);
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e){
      //  return ResultUtil.error(100,e.getMessage());

        if (e instanceof GirlException){
            GirlException girlException = (GirlException)e;
            return  ResultUtil.error(girlException.getCode(),girlException.getMessage());
        }else {
            logger.info("【系统异常】{}",e);
            return  ResultUtil.error(-1,"未知错误");
        }
    }
}
