Commit fe150613 by qiuweili123

增加异常扩展

parent 13f5872b
...@@ -4,8 +4,10 @@ import com.secoo.mall.common.core.errorcode.CommonErrorCode; ...@@ -4,8 +4,10 @@ import com.secoo.mall.common.core.errorcode.CommonErrorCode;
import com.secoo.mall.common.core.exception.BusinessException; import com.secoo.mall.common.core.exception.BusinessException;
import com.secoo.mall.common.core.exception.ParameterException; import com.secoo.mall.common.core.exception.ParameterException;
import com.secoo.mall.common.core.exception.SystemInternalException; import com.secoo.mall.common.core.exception.SystemInternalException;
import com.secoo.mall.common.processor.ExceptionProcessor;
import com.secoo.mall.common.util.log.LoggerUtil; import com.secoo.mall.common.util.log.LoggerUtil;
import com.secoo.mall.common.util.response.ResponseUtil; import com.secoo.mall.common.util.response.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource; import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
...@@ -17,23 +19,34 @@ public class ProtocolExceptionHandler { ...@@ -17,23 +19,34 @@ public class ProtocolExceptionHandler {
@Resource @Resource
private MessageSource messageSource; private MessageSource messageSource;
@ExceptionHandler(BusinessException.class) @Autowired(required = false)
public Object businessExcepstionHandler(BusinessException e) { private ExceptionProcessor exceptionProcessor;
LoggerUtil.warn("businessException info:", e);
return ResponseUtil.getFailResponse(e.getCode(), getMsg(e));
}
@ExceptionHandler(ParameterException.class)
public Object parameterExceptionHandler(ParameterException e) {
String msg = getMsg(e);
LoggerUtil.info(msg);
return ResponseUtil.getFailResponse(e.getCode(), msg);
}
@ExceptionHandler({SystemInternalException.class, Exception.class}) @ExceptionHandler({Exception.class})
public Object exceptionHandler(Exception e) { public Object exceptionHandler(Exception e) {
LoggerUtil.error(e); try {
return ResponseUtil.getFailResponse(CommonErrorCode.SYSTEM_INTERNAL_EXCEPTION.getCode(), CommonErrorCode.SYSTEM_INTERNAL_EXCEPTION.getMsg()); if (exceptionProcessor == null) {
throw e;
}
exceptionProcessor.process(e);
} catch (ParameterException ex) {
String msg = getMsg(ex);
LoggerUtil.info(msg);
return ResponseUtil.getFailResponse(ex.getCode(), msg);
} catch (SystemInternalException ex) {
LoggerUtil.error(e);
return ResponseUtil.getFailResponse(CommonErrorCode.SYSTEM_INTERNAL_EXCEPTION.getCode(), CommonErrorCode.SYSTEM_INTERNAL_EXCEPTION.getMsg());
} catch (BusinessException ex) {
LoggerUtil.warn("businessException info:", ex);
return ResponseUtil.getFailResponse(ex.getCode(), getMsg(ex));
} catch (Exception ex) {
LoggerUtil.error(ex);
return ResponseUtil.getFailResponse(CommonErrorCode.SYSTEM_INTERNAL_EXCEPTION.getCode(), CommonErrorCode.SYSTEM_INTERNAL_EXCEPTION.getMsg());
}
return null;
} }
......
package com.secoo.mall.common.processor;
public abstract class AbsExceptionProcessor implements ExceptionProcessor{
public void process(Exception e) throws Exception{
convertException(e);
throw e;
}
protected abstract void convertException(Exception e);
}
package com.secoo.mall.common.processor;
import com.secoo.mall.common.core.errorcode.ErrorCode;
import com.secoo.mall.common.core.exception.BusinessException;
import com.secoo.mall.common.util.spring.SpringUtil;
import java.util.Map;
public interface ExceptionProcessor {
/**
* 处理异常
* @param e
* @return
*/
void process(Exception e) throws Exception;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment