Skip to content

0.5.0: Handle invalid responses #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api-angular-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 29 additions & 18 deletions api-angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,23 +194,37 @@

// Parses the body of a JSON response and returns an
// array of objects.
var parseResponseBody = function(response) {
var parseResponseBody = function parseResponseBody(response) {
var results = [];
var responses = JSON.parse(response.body);
if (!(responses instanceof Array)) {
console.error("Expected array from response.body but got " + responses);
if (responses) {
throw new Error("Expected array from response.body but got " + responses);
}
return [];
}

// exceptions will be bubbled up
for (var i in responses) {
results.push(JSON.parse(responses[i]));
}
responses.forEach(function parseResponseInner(v) {
if (typeof v !== 'string') {
throw new Error('Expected string for inner JSON but got ' + JSON.stringify(v));
}
try {
results.push(JSON.parse(v));
} catch (e) {
console.error('Expected valid JSON string but got', e);
throw new Error('Invalid inner JSON string. ' + e.message + ' data: ' + v);
}
});

return results;
};


// Resolves all queued promises with the associated
// result from the API response.
var resolveQueue = function(queue, results) {
for (var i in queue) {
var resolveQueue = function resolveQueue(queue, results) {
for (var i = 0; i < queue.length; i += 1) {
var result = results[i];
// If the API returns nothing then assume it's ok.
if (result == null) {
Expand All @@ -236,16 +250,16 @@

// Rejects all the queued promises with the provided
// error.
var rejectQueue = function(queue, error) {
for (var i in queue) {
queue[i].deferred.reject(error);
}
var rejectQueue = function rejectQueue(queue, error) {
queue.forEach(function rejectElement(el) {
el.deferred.reject(error);
});

return error;
};

// Clears the queue of API calls and the batch timeout.
TaggedApi.prototype.resetQueue = function() {
TaggedApi.prototype.resetQueue = function resetQueue() {
if (null !== this._batchTimeout) {
clearTimeout(this._batchTimeout);
this._batchTimeout = null;
Expand Down Expand Up @@ -295,12 +309,9 @@
var stringifyQueue = function(queue) {
// Each API call will be transformed into a string of
// key/value pairs and placed into this array.
var calls = [];

for (var i in queue) {
var call = stringifyCall(queue[i]);
calls.push(call);
}
var calls = queue.map(function (el) {
return stringifyCall(el);
});

return "\n" + calls.join("\n") + "\n";
};
Expand Down
Loading