New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在service端,怎么处理 统一异常处理机制 #433

Open
longshang opened this Issue May 9, 2017 · 3 comments

Comments

Projects
None yet
2 participants
@longshang

longshang commented May 9, 2017

如题

@rayzhang0603

This comment has been minimized.

Show comment
Hide comment
@rayzhang0603

rayzhang0603 May 19, 2017

Collaborator

是想要对server端的异常统一拦截处理吗?可以在server端增加filter来处理,可以参考SPI扩展 以及com.weibo.api.motan.filter包下其他filter的实现

Collaborator

rayzhang0603 commented May 19, 2017

是想要对server端的异常统一拦截处理吗?可以在server端增加filter来处理,可以参考SPI扩展 以及com.weibo.api.motan.filter包下其他filter的实现

@longshang

This comment has been minimized.

Show comment
Hide comment
@longshang

longshang May 19, 2017

好的,多谢,我现在是这样实现的:
/**

  • motan 服务端错误拦截器,处理未异常503 40001
  • @author longshang

*/
@SpiMeta(name = "serviceException")
@ACTiVATiON(sequence = 30)
public class MotanExceptionFilter implements Filter {

public Response filter(Caller<?> caller, Request arg1) {
	if (caller instanceof Provider) { 
		Response result = null;

		result = caller.call(arg1);
		MotanBizException motanException = null;
		try {
			if (result == null || result.getException() != null) {
				// 给调用接口出现异常进行打点
				Throwable cause = result.getException();
				if (cause instanceof MotanBizException) {
					MotanErrorMsg errorMsg = null;
					if (cause.getCause() instanceof MotanBizException) {
						MotanBizException ex = (MotanBizException) cause.getCause();
						errorMsg = ex.getMotanErrorMsg();
					} else {
						errorMsg = new MotanErrorMsg(503, 40001, cause.getCause().getMessage());
					}
					motanException = new MotanBizException(errorMsg);
				} else {
					MotanErrorMsg errorMsg = new MotanErrorMsg(503, 40001, cause.getMessage());
					motanException = new MotanBizException(errorMsg);
				}
			}
		} catch (Exception e) {
		}
		if (motanException != null) {
			throw motanException;
		} else {
			return result;
		}

	}
	return null;
}

}

longshang commented May 19, 2017

好的,多谢,我现在是这样实现的:
/**

  • motan 服务端错误拦截器,处理未异常503 40001
  • @author longshang

*/
@SpiMeta(name = "serviceException")
@ACTiVATiON(sequence = 30)
public class MotanExceptionFilter implements Filter {

public Response filter(Caller<?> caller, Request arg1) {
	if (caller instanceof Provider) { 
		Response result = null;

		result = caller.call(arg1);
		MotanBizException motanException = null;
		try {
			if (result == null || result.getException() != null) {
				// 给调用接口出现异常进行打点
				Throwable cause = result.getException();
				if (cause instanceof MotanBizException) {
					MotanErrorMsg errorMsg = null;
					if (cause.getCause() instanceof MotanBizException) {
						MotanBizException ex = (MotanBizException) cause.getCause();
						errorMsg = ex.getMotanErrorMsg();
					} else {
						errorMsg = new MotanErrorMsg(503, 40001, cause.getCause().getMessage());
					}
					motanException = new MotanBizException(errorMsg);
				} else {
					MotanErrorMsg errorMsg = new MotanErrorMsg(503, 40001, cause.getMessage());
					motanException = new MotanBizException(errorMsg);
				}
			}
		} catch (Exception e) {
		}
		if (motanException != null) {
			throw motanException;
		} else {
			return result;
		}

	}
	return null;
}

}

@rayzhang0603

This comment has been minimized.

Show comment
Hide comment
@rayzhang0603

rayzhang0603 May 19, 2017

Collaborator

我其实不太了解你的拦截异常的实际业务场景,实际实现中有下面几个建议可以参考。
1、filter尽量能兼容client端和server端,即使client端误用也不会产生影响。
2、尽量不要直接返回null,最好是封装一个带异常的response
3、ProviderMessageRouter也会对未知异常进行统一封装,可以通过debug调试不同异常实际返回给client端的结果。

Collaborator

rayzhang0603 commented May 19, 2017

我其实不太了解你的拦截异常的实际业务场景,实际实现中有下面几个建议可以参考。
1、filter尽量能兼容client端和server端,即使client端误用也不会产生影响。
2、尽量不要直接返回null,最好是封装一个带异常的response
3、ProviderMessageRouter也会对未知异常进行统一封装,可以通过debug调试不同异常实际返回给client端的结果。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment