package com.kexue.skills.interceptor;

import com.kexue.skills.common.*;
import com.kexue.skills.exception.BizException;
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;

@Slf4j
@ControllerAdvice
public class BizExceptionAdvice {
    @ExceptionHandler(value = BizException.class)
    @ResponseBody
    public CommonResult handleException(BizException e){
        e.printStackTrace();
        if (null != e && null != e.getMessage()){
            log.error("服务内部异常:{}",e.getMessage());
            if (e.getMessage().indexOf("Data too long") > 0){
                return CommonResult.failed(e.getErrorCode(),"属性长度超过数据库限制！");
            }
            return CommonResult.failed(e.getErrorCode(),e.getMessage());
        }
        return CommonResult.failed("未知错误，请联系管理员！");
    }
}
