viezel / napp.alloy.adapter.restapi

RestAPI Sync Adapter for Titanium Alloy Framework
197 stars 102 forks source link

No error object when request is failing... #51

Open john-dalsgaard opened 9 years ago

john-dalsgaard commented 9 years ago

Hi

I am trying to use this API for retrieving and updating data. If I do a collection.create and it fails, the code returns to the "error:" section. However, I have found no way to get a handle on the error message itself...

This is my code:

var model = Alloy.createCollection('Service');
model.create(buildServiceRequest(entity,dataType,action),{
    success: function(result){
        console.info("sendPostRequest result.toJSON(): " + JSON.stringify(result.toJSON()));
        // var response = (result && result.attributes) ? reßsult.attributes : null;
        var response = result.toJSON();
        if(response.success) {
            console.debug("call success callback");
            if(callBack) {
                callBack(response);
            }
        } else {
            console.debug("call error callback");
            if(errorCallBack) {
                errorCallBack(response);
            }
        }
    },
    error: function(result, x, e){
        var response = result.toJSON();
        console.debug("sendPostRequest response: " + JSON.stringify(response));
        console.error("sendPostRequest error: " + JSON.stringify(e));
        if(errorCallBack) {
            errorCallBack(response,e);
        }
    }
}); // Save data

'x' seemt to always be undefined. The output from the above code is:

[INFO] [iphone, 8.0, 192.168.42.38] sendPostRequest response: {"action":"login","dataType":"Credentials","data":{"user":"john@dalsgaard-data.dk","password":"dfdf","version":"1.0.0"}} [ERROR] [iphone, 8.0, 192.168.42.38] sendPostRequest error: {"collection":[{"action":"login","dataType":"Credentials","data":{"user":"john@dalsgaard-data.dk","password":"dfdf","version":"1.0.0"}}],"i ndex":0} [INFO] [iphone, 8.0, 192.168.42.38] remoteLoginError: result.success=undefined, result.error=undefined [ERROR] [iphone, 8.0, 192.168.42.38] result={"action":"login","dataType":"Credentials","data":{"user":"john@dalsgaard-data.dk","password":"dfdf","version":"1.0.0"}} [ERROR] [iphone, 8.0, 192.168.42.38] [REST API] CREATE ERROR: [ERROR] [iphone, 8.0, 192.168.42.38] { "success": false, "status": "error", "code": 403, "error": "HTTP error", "responseText": "{\"success\":false,\"error\":{\"id\":\"2\",\"message\":\"Ugyldig bruger (john@dalsgaard-data.dk) eller kodeord\"}}", "responseJSON": { "success": false, "error": { "id": "2", "message": "Ugyldig bruger (john@dalsgaard-data.dk) eller kodeord" } } }

So basically, I don't get the error back in my code. However, I can see it from the REST API as I have enabled debugging.

If I use a "fetch" instead and this error handling:

 var dataLastChanged = Alloy.createCollection('UserDataLastChanged');
    dataLastChanged.fetch({
        success : function(){
            _.each(dataLastChanged.models[0].attributes, function(element, index, list){
                // Loop through the first model returned. In the attributes we find the key/value pairs we are looking for
                :
                :
                  }
            });
        },
        error: function(result, e){
            console.error("Error msg: " + e);
        }
    }); // Grab data 

Then I get the valid error message (though only the message in localized language - and not a code to make it easier to check programmatically). Am I doing something wrong? - or does the API not support returning the "real" error to my code?

Thanks in advance.

Edited 23 October - more complete code examples...

/John

SPorwal commented 8 years ago

@john-dalsgaard, I am facing the similar issue, did you get any work around?

john-dalsgaard commented 8 years ago

@SPorwal No, unfortunately not. So right now I just get "an error" and reacts the same way to all errors. I have looked at other APIs to do the REST calls - but so far I have not changed anything.

I guess we could fix it ourselves and send a pull request :-)