Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    RestDataSource.transformResponse - addHandleErrorHandler not firing negative status

    Hi,

    I'm using Smart GWT 2.1 with a JSON RestDataSource. I'm hoping the issue here is simply my lack of understanding.

    I'm basically trying to catch any negative status codes raised by the transformResponse method and it doesn't seem to be working.

    I have extended RestDataSource to include JSON bindings and the transformResponse method. In the transformResponse method I basically have a test where I'm simply setting
    Code:
    response.setStatus(-1)
    .

    I instantiate the datasource in another class with the following:
    Code:
    DataSource loginDataSource = new JsonPostMessageRestDataSource();
    ...
    		loginDataSource.addHandleErrorHandler(new HandleErrorHandler() {
    			
    			@Override
    			public void onHandleError(ErrorEvent event)
    			{
    				DSResponse response = event.getResponse();
    			}
    		});
    I know for sure the datasource is working and setting the response status in transformResponse to -1 as I'm tracing it in Eclipse. Yet for some reason the handler never fires (I have a breakpoint on it).

    Do I have to create a custom ErrorEvent and fire it myself in transformResponse in order for this to work? And if so, how do I do that?

    Any help is greatly appreciated.

    As an aside, in the javadoc for DataSource.addHandleErrorHandler, I am completely confused by what it means by "if you define this method" and "return false from this method".

    "If you define this method on a DataSource, it will be called whenever the server returns a DSResponse with a status other than RPCResponse.STATUS_SUCCESS. You can use this hook to do DataSource-specific error handling. Unless you return false from this method, com.smartgwt.client.rpc.RPCManager#handleError will be called by Smart GWT right after this method completes."

    #2
    How to handle error responses from a RestDataSource

    Hi again,

    I have a feeling my original post was a bit verbose and confusing.

    Basically I'm trying to figure out how to handle negative status codes myself using a RestDataSource.

    I already setup the transformResponse method to return a negative status code (like -1), but I can't figure out what callback/handler I need to implement in Java that gets sent the event.

    Thanks.

    Comment


      #3
      I've got same problem. I need an error message to display (generated on server side) and make sure e.g. when I do remove operation the record does not disappear from listgrid.
      Can somebody help?

      Thanks,
      Zemi

      Comment


        #4
        Why don't u try using status = 0 as success, negative leave for system errors (SmartGWT) and positive for your own user handled errors - works for me

        Regards
        Jacek

        Originally posted by matuszemi
        I've got same problem. I need an error message to display (generated on server side) and make sure e.g. when I do remove operation the record does not disappear from listgrid.
        Can somebody help?

        Thanks,
        Zemi

        Comment


          #5
          That doesn't work. Setting status to a positive number doesn't trigger the callback, at least not in Power edition, snapshot 13-12-2010.

          I am also looking for a solution for this, since my client side needs to do some stuff after a certain error is made...

          Any ideas?

          This is my client code:
          Code:
          twDataSource.addHandleErrorHandler(new HandleErrorHandler(){
          	public void onHandleError(ErrorEvent event){
          		int status = event.getResponse().getStatus();
          		if(status == 1231){
          			// do something
                          }
          	}
          			
          });
          And my serverside:

          Code:
          public class MyCustomDataSource extends BasicDataSource {
          	@Override
          	public DSResponse executeAdd(DSRequest req) throws Exception { 
                      if(somethingSpecificWentWrong()){
          	       DSResponse res = new DSResponse();
          	       res.setStatus(1231);//lets use positive numbers for our errors
          	       return res;
                      }
          	}  
          	//...
          }
          Last edited by Sytematic; 17 Dec 2010, 09:03.

          Comment


            #6
            Setting a negative status code triggers central error handling (see RPCManager "handleError") unless dsRequest.willHandleError has been set.

            Comment


              #7
              So, to the people having troubles with this, this is how you should do it:

              1) on your custom DataSource, return with a negative status number, or simply setFailure();
              Code:
              DSResponse res = new DSResponse();
              res.setStatus(-123);
              return res;
              2) call your DataSource like this, if it is, say, from a DynamicForm:
              Code:
              DSRequest req = new DSRequest();
              req.setWillHandleError(true);						
              form.saveData(new DSCallback(){
              	public void execute(DSResponse response,
              			Object rawData, DSRequest request) {
              		SC.say("DSCallback");						
              	}				
              },req);

              Comment

              Working...
              X