package com.huatek.busi.exception;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Maps;
import com.huatek.torch.frame.exception.OwlException;
import com.huatek.torch.frame.tools.ConstantUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * 统一异常处理
 * 
 * @author anker_cheng
 * @date 2019/08/21
 */
@Slf4j
@ResponseBody
@ControllerAdvice(annotations = RestController.class)
public class BusiExceptionHandler {

    /**
     * 顶级异常
     * 
     * @param e
     * @return
     */
    @ExceptionHandler({Exception.class})
    public Map<String, Object> handleException(HttpServletRequest request, HttpServletResponse response, Exception e) {
        Map<String, Object> result = Maps.newLinkedHashMapWithExpectedSize(3);
        result.put("code", ConstantUtil.REQUEST_FAIL);
        result.put("msg", "系统异常，请联系管理员");
        result.put("token", "");
        log.error(e.getMessage(), e);
        return result;
    }

    @ExceptionHandler({OwlException.class})
    public Map<String, Object> handleOwlException(Exception e) {
        Map<String, Object> result = Maps.newLinkedHashMapWithExpectedSize(3);
        result.put("code", ConstantUtil.REQUEST_FAIL);
        result.put("msg", e.getMessage());
        result.put("token", "");
        log.debug(e.getMessage());
        return result;
    }
}
