handling GWT RequestFactory server error responses

吃可爱长大的小学妹 提交于 2019-12-03 08:27:37

问题


I have a newly coded GWT/GAE app that uses RequestFactory and Editors on the client and a custom Objectify DAO Service on the back.

The flush() then persist() paths work fine on success. Client side JSR 303 works as well as can be expected too.

My question is how to trigger server warnings/errors and handle UI updates?

I am using Chandler's Generic DAO for Objectify 2 at http://turbomanage.wordpress.com/2010/02/09/generic-dao-for-objectify-2/

my gwt activity is calling persist( myProxy ).fire( new Receiver<> )

my dao code is throwing IllegalArgumentException and other RuntimeExceptions for business logic situations like "Duplicate email address found - want to login instead?"

Receiver<>.onSuccess() works fine to track a successful outcome. neither Receiver<>.onFailure() nor Receiver<>.onViolation() report the RuntimeExceptions.

( Correction: onFailure() is being called for server-side exceptions)

Is there a better way to do this? What exceptions should the DAO throw such that onViolation() or onFailure() report errors? How should the editor(s) handle and recover from the exception?


回答1:


I've found the most versatile command sequence to be

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();
}

Based on the conversation excerpt below at 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…



来源:https://stackoverflow.com/questions/5277084/handling-gwt-requestfactory-server-error-responses

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!