处理 GWT RequestFactory 服务器错误响应

23

我有一个新编写的 GWT/GAE 应用程序,客户端使用 RequestFactory 和 Editors,在后端使用自定义的 Objectify DAO Service。

成功时,flush() 和 persist() 路径都可以正常工作。客户端 JSR 303 也能如预期地工作。

我的问题是如何触发服务器警告/错误并处理 UI 更新?

我正在使用 Chandler 的 Objectify 2 通用 DAO,在 http://turbomanage.wordpress.com/2010/02/09/generic-dao-for-objectify-2/ 中可找到。

我的 GWT 活动通过调用 persist(myProxy).fire(new Receiver<>) 来持久化对象。

我的 DAO 代码在业务逻辑情况下(例如:“找到重复的电子邮件地址 - 是否要登录?”)抛出 IllegalArgumentException 和其他 RuntimeExceptions。

Receiver<>.onSuccess() 可以很好地跟踪成功的结果。但是 Receiver<>.onFailure() 和 Receiver<>.onViolation() 都不能报告 RuntimeExceptions。

(更正:onFailure() 会应对服务器端异常)

是否有更好的方法来处理这个问题? 在什么情况下,DAO 应该抛出异常,以便 onViolation() 或 onFailure() 报告错误? 编辑器应该如何处理和从异常中恢复?

1个回答

0

我发现最通用的命令序列是

void start() {
    // Either get p
    context1.get(..).to( new Receiver<P> { onSuccess(P resp){p = resp;} ... }).fire();
    // OR create p
    p = context2.create( P.class );
    // Then save p
    req = context2.persist(p).to( new Receiver<P>{  /* note do not use context1 */
        onViolation(...) { /*JSR 303 handler*/ };
        onFailure( error ) { /* handle */ error.getMessage() }; 
        onSuccess(X x) { /* whatever persist() returns handler */ }; } ); 
    // drive editor with p
    driver.edit( p, req);    
}

....
void onSave() {    
    // editor
    ctxt = driver.flush()  /* note ctxt == context2 */
    if ( driver.hasErrors() ) { /*JSR 303 handler*/};
    // RF
    ctxt.fire();
}

基于以下对话摘录 http://groups.google.com/group/google-web-toolkit/browse_thread/thread/da863606b3893132/96956661c53e1064?hl=en

Thomas Broyer onFailure 应该包含在服务器端抛出的 exception 的 getMessage()。 您可以通过向 RequestFactoryServlet 提供自己的 ExceptionHandler 来进行微调(扩展它并使用其构造函数接受一个 ExceptionHandler)。 只有当您的实体未通过 JSR-303 Bean 验证(在调用任何服务方法之前检查)时,才会调用 onViolation 。 如果您想在客户端代码中“捕获”失败,则必须为 persist() 服务方法添加接收器:
context.persist(p).to(new Receiver…


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接