java - 处理 GWT RequestFactory 服务器错误响应

标签 java google-app-engine gwt dao

我有一个新编码的 GWT/GAE 应用程序,它在客户端使用 RequestFactory 和 Editors,在背面使用自定义 Objectify DAO 服务。

flush() 然后 persist() 路径在成功时可以正常工作。 客户端 JSR 303 也可以正常工作。

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

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

我的 gwt Activity 正在调用 persist(myProxy).fire(new Receiver<>)

我的 dao 代码抛出 IllegalArgumentException 和其他 RuntimeExceptions 用于业务逻辑情况,例如“发现重复的电子邮件地址 - 想要登录?”

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

(更正:针对服务器端异常调用 onFailure())

有没有更好的方法来做到这一点? DAO 应该抛出哪些异常以使 onViolation() 或 onFailure() 报告错误? 编辑器应如何处理异常并从异常中恢复?

最佳答案

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

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 should containg the getMessage() of the exception you threw on the server side.

You can tweak it by providing your own ExceptionHandler to the RequestFactoryServlet (extend it and use its constructor taking an ExceptionHandler).

onViolation will only be called if your entities do not pass JSR-303 Bean Validation, which is checked before calling any service method.

If you want to "catch" the failure in clidnt code, you have to add a Receiver for the persist() service method:
context.persist(p).to(new Receiver…

关于java - 处理 GWT RequestFactory 服务器错误响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5277084/

相关文章:

java - 内存增加是否会导致 java 应用程序的输出或行为发生差异?

node.js - 已部署的 Bookshelf 教程应用程序 (App Engine/NodeJS) 的 CloudSQL 连接问题

python - 让 pubsubhubbub 集线器工作

java - 在 Google App Engine 中存储旋转/翻转图像

json - 如何在 GWT 项目中将 JSON 转换为 Java 对象,反之亦然?

java - GWT 垂直菜单

java - 无法连接到 sqlite 数据库 - _syscall() 中出现内部错误

java - Android:100+ KB 的文件下载变成 1 KB

java - 将 ArrayList 元素克隆到同一个 ArrayList

java - 套接字通信中客户端输入之间的时间