Announcement

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

    Accessing the response "data" attribute to retrieve error message

    Hi,
    I am wondering how can I retrieve (at client side, from the RPCManager HandleErrorCallback) the contents of the response "data" attribute when it contains an error message (as suggested in various threads for generic errors not related to validation failures).

    In practice, I have a RestDataSource, that's getting a JSON response with the attribute "status" to -1 and the "data" attribute containing the error message:
    Code:
    {"response":{"status":-1,"data":"Internal server error: java.lang.RuntimeException with message test error message"}}
    I'm using the HandleErrorCallback only to have a general way to format the contents of the error message dialog, and let the dialog show only the error message, as (I think by default) it shows the entire response contents:
    Code:
    Transport error: 500 - {"response":{"status":-1,"data":"Internal server error: java.lang.RuntimeException with message test error message"}}
    The DSResponse.getData() is not compatible cause it offers a Record array, and with response.getAttribute ("data") I only get a dialog saying "[object OBJECT]"...

    This is a snippet of my last HandleErrorCallback experiment
    Code:
    RPCManager.setHandleErrorCallback (new HandleErrorCallback() {
    	
    	@Override
    	public void handleError (DSResponse response, DSRequest request) {
    		String errorMsg;
    		try {
    			errorMsg = response.getAttribute ("data");
    		} catch (final Exception e) {
    			errorMsg = null;
    		}
    		if (errorMsg!=null) {
    			SC.warn (StringUtil.abbreviate (errorMsg.toString (), 200));
    		} else {
    			SC.warn ("Internal server error");
    		}
    	}
    });
    At client side I'm using
    SmartClient Version: 8.0/LGPL Development Only (built 2010-05-18)
    GWT 2.0.4
    Firefox 3.6.9

    at server side Grails 1.3.4

    Kind regards
    Davide
    Last edited by d.cavestro; 4 Oct 2010, 04:06. Reason: Editing title to clarify the question

    #2
    When you signal a 500 server error we're assuming the server completely crashed and the response may be gibberish. If you instead signal 200 OK at the HTTP level but send back a negative status code, the behavior of displaying just the "data" attribute in a dialog is automatic, no override needed.

    If you must send 500 server error for whatever reason, you can get the full response from the DSResponse object, parse it as JSON, and show just the data attribute yourself.

    Comment


      #3
      Ok, I've got it.

      Many thanks
      Davide

      Originally posted by Isomorphic
      When you signal a 500 server error we're assuming the server completely crashed and the response may be gibberish. If you instead signal 200 OK at the HTTP level but send back a negative status code, the behavior of displaying just the "data" attribute in a dialog is automatic, no override needed.

      If you must send 500 server error for whatever reason, you can get the full response from the DSResponse object, parse it as JSON, and show just the data attribute yourself.

      Comment


        #4
        Ok,
        I made as you suggested and IT WORKS.

        The following code uses the response "data" attribute to get the error message from server even in case of a 500 error, with a graceful degradation if the response is actually gibberish:
        Code:
        RPCManager.setHandleTransportErrorCallback (new HandleTransportErrorCallback () {
        
        	@Override
        	public void handleTransportError (final int transactionNum, final int status, final int httpResponseCode, final String httpResponseText) {
        		try {
        			final JSONObject responseValue = JSONParser.parse (httpResponseText).isObject ().get ("response").isObject ();
        			final String errorMessage = responseValue.get ("data").isString ().stringValue ();
        			SC.warn (errorMessage);
        		} catch (final Exception e) {
        			GWT.log ("Cannot show an appropriate message for a transport error (server response broken?)", e);
        			SC.warn ("Transport error: " + httpResponseCode + " - " + httpResponseText);
        		}
        	}
        });
        It uses the com.google.gwt.json.client.JSONParser from GWT, hence I added to the module descriptor the following declaration
        Code:
        	<!-- Inherit the client side JSON handling. -->
        	<inherits name='com.google.gwt.json.JSON'/>
        Just hope this example could be useful to someone else.

        Cheers
        Davide

        Comment

        Working...
        X