1 package autotest.common;
2 
3 import autotest.common.ui.NotifyManager;
4 
5 import com.google.gwt.json.client.JSONObject;
6 import com.google.gwt.json.client.JSONString;
7 import com.google.gwt.json.client.JSONValue;
8 
9 /**
10  * One of onSuccess() and onError() is guaranteed to be called for every RPC request.
11  */
12 public abstract class JsonRpcCallback {
13     /**
14      * Called when a request completes successfully.
15      * @param result the value returned by the server.
16      */
onSuccess(JSONValue result)17     public abstract void onSuccess(JSONValue result);
18 
19     /**
20      * Called when any request error occurs
21      * @param errorObject the error object returned by the server, containing keys "name",
22      * "message", and "traceback".  This argument may be null in the case where no server response
23      * was received at all.
24      */
onError(JSONObject errorObject)25     public void onError(JSONObject errorObject) {
26         if (errorObject == null) {
27             return;
28         }
29 
30         String errorString =  getErrorString(errorObject);
31         JSONString tracebackString = errorObject.get("traceback").isString();
32         String traceback = null;
33         if (tracebackString != null) {
34             traceback = tracebackString.stringValue();
35         }
36 
37         NotifyManager.getInstance().showError(errorString, traceback);
38     }
39 
getErrorString(JSONObject errorObject)40     protected String getErrorString(JSONObject errorObject) {
41         if (errorObject == null) {
42             return "";
43         }
44 
45         String name = Utils.jsonToString(errorObject.get("name"));
46         String message = Utils.jsonToString(errorObject.get("message"));
47         return name + ": " + message;
48     }
49 }
50