我的应用程序有许多RPC调用,它们都有一个.onFailure(可抛出捕获)方法。我有一个在客户端和服务器代码之间共享的类 不记录在例外中。如果用户没有基于会话/cookie/权限等的相关权限,则由服务器抛出。
理想情况下,我希望在将其他异常传递给.onFailure()代码之前,在一个地方处理此异常,因为此处理是如此普遍,而且出于安全考虑,需要如此。有一个GWT。setUncaughtExceptionHandler(),但这似乎是在处理后调用的,这并不理想(如果.onFailure意外消耗了太多)。
有没有人对此有一个优雅的解决方案?一个丑陋的解决方案是将延迟绑定的 .create() 代理包装在实现异步接口的同一聚合类中。
附言:服务器之前发出了重定向,但我不喜欢这种范式,更希望由应用程序的事件总线处理。
更新:上面提到的丑陋答案
公共抽象类CustomAsyncCallback实现AsyncCarlback{
@Override
public CustomAsyncCallback(AsyncCallback<T> callback)
{
this.wrap = callback ;
}
AsyncCallback<T> wrap ;
@Override
public void onFailure(Throwable caught) {
if (!handleException())
{
wrap.onFailure(caught) ;
}
}
@Override
public void onSuccess(T t) {
wrap.onSuccess(t) ;
}
}
公共类WrapDeferredBinding实现RpcInterfaceAsync{RpcInterface Async service=GWT.create(RpcInternet.class);
public void method1(int arg1, AsyncCallback<Boolean> callback)
{
service.method1(arg1, new CustomAsyncCallback<Boolean>(callback)) ;
}
public void method2 ....
public void method3 ....
}
您可以使用抽象类包装异步回调
类:
public abstract class CustomAsyncCallback<T> implements AsyncCallback<T>{
@Override
public void onFailure(Throwable caught) {
GWT.log(caught.getMessage());
handleException();
this.customOnFailure(yourDesireParam);
}
/**
* this method is optional
*/
public abstract void customOnFailure(Param yourDesireParam);
}
然后将CustomAsyncCallback
对象发送到您的RPC异步方法。
为了包装每个AsynCallback
首先,您需要定义自己的代理生成器,以便在每次生成< code>RemoteService
代理时介入。首先扩展< code > ServiceInterfaceProxyGenerator 并重写< code > # createProxyCreator()。
/**
* This Generator extends the default GWT {@link ServiceInterfaceProxyGenerator} and replaces it in the
* co.company.MyModule GWT module for all types that are assignable to
* {@link com.google.gwt.user.client.rpc.RemoteService}. Instead of the default GWT {@link ProxyCreator} it provides the
* {@link MyProxyCreator}.
*/
public class MyServiceInterfaceProxyGenerator extends ServiceInterfaceProxyGenerator {
@Override
protected ProxyCreator createProxyCreator(JClassType remoteService) {
return new MyProxyCreator(remoteService);
}
}
在您的< code>MyModule.gwt.xml中,使用延迟绑定来指示gwt在生成< code>RemoteService类型的内容时使用您的代理生成器进行编译:
<generate-with
class="com.company.ourapp.rebind.rpc.MyServiceInterfaceProxyGenerator">
<when-type-assignable class="com.google.gwt.user.client.rpc.RemoteService"/>
</generate-with>
扩展ProxyCreator并覆盖#getProxySupertype()
。在MyServiceInterfaceProxyGenerator#createProxyCreator()
中使用它,以便您可以为所有生成的RemoteServiceProxies定义基类。
/**
* This proxy creator extends the default GWT {@link ProxyCreator} and replaces {@link RemoteServiceProxy} as base class
* of proxies with {@link MyRemoteServiceProxy}.
*/
public class MyProxyCreator extends ProxyCreator {
public MyProxyCreator(JClassType serviceIntf) {
super(serviceIntf);
}
@Override
protected Class<? extends RemoteServiceProxy> getProxySupertype() {
return MyRemoteServiceProxy.class;
}
}
确保您的< code>MyProxyCreator和< code > MyServiceInterfaceProxyGenerator 都位于不会被GWT交叉编译为javascript的包中。否则,您将看到如下错误:
[ERROR] Line XX: No source code is available for type com.google.gwt.user.rebind.rpc.ProxyCreator; did you forget to inherit a required module?
您现在已准备好扩展远程服务代理
并覆盖#doCreateRequestCallback()
!在这里,您可以做任何您喜欢的事情,并将其应用于进入服务器的每个回调。确保将此类以及您在此处使用的任何其他类(在我的例子中为 AsyncCallbackProxy)
添加到要交叉编译的客户端包中。
/**
* The remote service proxy extends default GWT {@link RemoteServiceProxy} and proxies the {@link AsyncCallback} with
* the {@link AsyncCallbackProxy}.
*/
public class MyRemoteServiceProxy extends RemoteServiceProxy {
public MyRemoteServiceProxy(String moduleBaseURL, String remoteServiceRelativePath, String serializationPolicyName,
Serializer serializer) {
super(moduleBaseURL, remoteServiceRelativePath, serializationPolicyName, serializer);
}
@Override
protected <T> RequestCallback doCreateRequestCallback(RequestCallbackAdapter.ResponseReader responseReader,
String methodName, RpcStatsContext statsContext,
AsyncCallback<T> callback) {
return super.doCreateRequestCallback(responseReader, methodName, statsContext, new AsyncCallbackProxy<T>(callback));
}
}
现在,您的< code>AsyncCallbackProxy可以看起来像这样:
public class AsyncCallbackProxy<T> implements AsyncCallback<T> {
private AsyncCallback<T> delegate;
public AsyncCallbackProxy(AsyncCallback<T> delegate) {
this.delegate = delegate;
}
@Override
public final void onFailure(Throwable caught) {
GWT.log("AsyncCallbackProxy#onFailure() : " + caught.getMessage(), caught);
if (caught instanceof NotLoggedInException) {
// Handle it here
}
delegate.onFailure(proxy);
}
@Override
public final void onSuccess(T result) {
delegate.onSuccess(result);
}
}
参考资料:
异常Exception 以传统的try,catch抓取异常 如果在业务层不catch,框架层会捕捉,并返回一个500的server error响应。 如果在开发环境会返回一个500的具体错误的trace响应。 try { throw new \Exception("Error Processing Request", 1); //yield throwExc
异步Tcp客户端 异步Http客户端 异步Redis客户端 异步Mysql客户端 异步Log日志 异步文件读写 异常Exception
异步Log日志 use AsyncLog; yield AsyncLog::info('hello world'); yield AsyncLog::debug('test debug', ['foo' => 'bar']); yield AsyncLog::notice('hello world',[], 'group.com'); yield Async
我正在将本地netty服务器连接到远程https服务器以代理请求。 下面是我如何创建ssLcontext bean 当我点击我的本地主机时,它应该代理到后端。我得到以下异常。但是,如果SSL关闭并且我连接到远程,这是在不同端口上运行的本地服务器,则工作正常 编辑 添加的日志
我的项目是基于spring mvc的,我写了一个拦截器来拦截请求,我想从请求中获取参数,下面是我的代码: 但现在它抛出了一个异常: 出现例外的原因是什么?
异步文件读写 读文件 use AsyncFile; $content = (yield AsyncFile::read(__ROOT__."runtime/test.txt")); 写文件 $res = (yield AsyncFile::write(__ROOT__."runtime/test.txt", "hello wordls!")); $res = (yi