Search code examples
exceptiongwteditorrequestfactory

gwt with RequestFactory, handling server side exception on client and RequestFactoryEditorDriver


i use RequestFactory for communicating with server and RequestFactoryEditorDriver on the client side. So editing workflow looks like such way. Create new proxy for editing:

RequestContext reqCtx = clientFactory.getRequestFactory().Request();
UserAndAccountProxy userAndAccountProxy = reqCtx.create(UserAndAccountProxy.class);
reqCtx.saveAndReturnProfileAndAccount(userAndAccountProxy).to(
    new Receiver<UserAndAccountProxy>() {
      @Override
      public void onSuccess(UserAndAccountProxy response) {
      ...
      }

      @Override
      public void onFailure(ServerFailure error) {
        ...
      }}

And Save button click handling:

    RequestContext reqCtx = view.getEditorDriver().flush();
    reqCtx.fire();

On server side saveAndReturnProfileAndAccount method can throw exceptions on persisting, which i can handle in onFailure method. After that if I create new proxy with new request context and pass it to my editor all fields will be blanked. So what is proper way to execute request and if something goes wrong use data that user allready fill or maybe I made mistake in my editing worklow?


Solution

  • So, I think, I found solution. I made changes to function, which create RequestContext:

      private void edit(MyProxy proxy) {
    
        RequestContext reqCtx = clientFactory.getRequestFactory().Request();
    
        if (proxy == null) {
          // create proxy first time
          proxy = reqCtx.create(UserAndAccountProxy.class);
        } else {
          // create immutable copy
          proxy = reqCtx.edit(proxy);
        }
    
        final UserAndAccountProxy tmp = proxy;
    
        reqCtx.saveAndReturnMyProxy(proxy).to(new Receiver<MyProxy>() {
    
          @Override
          public void onFailure(ServerFailure error) {
            eventBus.showErrorInformation(error.getMessage());
            //recursive call with already filled proxy
            edit(tmp);
          }
    
          @Override
          public void onSuccess(UserAndAccountProxy response) {
            eventBus.showInformation("It`s ok!");
            eventBus.goToMainPage(null);
          }
        });
        // start editing with editor
        getView().onEdit(tmp, reqCtx);
      }
    

    When we start editing proxy function edit need to bee called with null argument and new clean proxy will be created. After that we start edit it with Editor. On Save button click we execute request to server. If it ends with success - we open another page. If request ends with error, we create new immutable copy ant push it to editor.