jasonkneen / RESTe

A simple JavaScript REST / API helper for Titanium

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

error handler: Choose between retry or callback

pakoArtal opened this issue · comments

Hi Jason,

I have problems understanding how to use retry & callback at the same time in the onError handler. How do I call callback from globalError ?

Using your example I have:

var reste = require("reste");
var api = new reste();

// now we can do our one-time configure
api.config({
    debug: true, // allows logging to console of ::REST:: messages
    errorsAsObjects: true, // Default: false. New in 1.4.5, will break 1.4.4 apps that handle errors
    autoValidateParams: false, // set to true to throw errors if <param> url properties are not passed
    validatesSecureCertificate: false, // Optional: If not specified, default behaviour from http://goo.gl/sJvxzS is kept.
    timeout: 4000,
    url: "https://api.parse.com/1/",
    requestHeaders: {
        "X-Parse-Application-Id": "APPID",
        "X-Parse-REST-API-Key": "RESTID",
        "Content-Type": "application/json"
    },
    methods: [{
        name: "courses",
        post: "functions/getCourses"
    }, {
        name: "getVideos",
        get: "classes/videos"
    }, {
        name: "getVideoById",
        get: "classes/videos/<videoId>"
    }, {
        name: "addVideo",
        post: "classes/videos"
    }],
    onError: globalError,
    onLoad: function(e, callback) {
        callback(e);
    }
});
function globalError(e, retry) {
	var dialog = Ti.UI.createAlertDialog({
		cancel : 1,
		title: "Connection error",
        message: "There was an error connecting to the server, check your network connection and  retry.",
        buttonNames :['Retry','Cancel']
	});

	dialog.addEventListener("click", function(e) {
		if (e.index === 0) {
			retry();
		} else {
			callback(e); // how do I do this??
		}
	});
	dialog.show();
}

And I call it like this:

api.getVideoById({
    videoId: "fUAM4ZFj9X"    
}, function(video) {
    // do stuff with the video
}, function(error) {
    // do stuff with the ERROR
});

Thanks,
Paco Artal

Hello Paco,
I have the same problem. It's a year later but have you find any way to call a specific error callback please ?
Or perhaps this snippet below is working ?
api.getVideoById({ videoId: "fUAM4ZFj9X" }, function(video) { // do stuff with the video }, function(error) { // do stuff with the ERROR });

Thanks !
Vincent

So onError doesn't have a callback because it's errored so the call is done -- in the example above you do nothing because you don't need to do anything. the retry() method will do the same call again (in case it was a network error etc).

Only the onLoad has the callback method because you want to intercept the onLoad, do something to the data and then continue (you might want to manipulate values / remove properties before passing back to RESTe so it can create the models / collections etc)