kiva / backbone.siren

Converts Siren JSON representations into Backbone Models
MIT License
14 stars 3 forks source link

Fix assumption that responseText will be JSON #31

Closed gxxcastillo closed 11 years ago

gxxcastillo commented 11 years ago

The JSON.parse(jqXhr.responseText || '{}') line assumes responseText will be a json object. However, there is a chance it will not be (such as in some 404 responses).

Backbone.Siren.ajax(rootUrl, options)
                            .done(function (entity) {
                                var bbSiren = Backbone.Siren.parse(entity);
                                deferred.resolve(bbSiren);
                                handleRootRequestSuccess(bbSiren, chain, chainedDeferred, options);
                            })
                            .fail(function (jqXhr) {
                                var entity = JSON.parse(jqXhr.responseText || '{}')
                                , bbSiren = Backbone.Siren.parse(entity);

                                deferred.reject(bbSiren, jqXhr);
                                chainedDeferred.reject(bbSiren, jqXhr);
                            });