Skip to content

Commit

Permalink
[lib] Provide default error message when server does not respond.
Browse files Browse the repository at this point in the history
  • Loading branch information
mwbrooks committed Apr 15, 2013
1 parent d52a31a commit 45e3d67
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/api.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ module.exports = function(options) {
args.callback(e); args.callback(e);
} }
else if (res.statusCode !== 200 && res.statusCode !== 201 && res.statusCode !== 202) { else if (res.statusCode !== 200 && res.statusCode !== 201 && res.statusCode !== 202) {
// provide a default message when none is provided
body = body || 'server returned status code ' + res.statusCode;

// eror in response // eror in response
args.callback(new Error(body)); args.callback(new Error(body));
} }
Expand Down
2 changes: 2 additions & 0 deletions lib/auth.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ module.exports = function(options, callback) {


// failed response // failed response
if (response.statusCode !== 200) { if (response.statusCode !== 200) {
// provide a default message when none is provided
body = body || 'server returned status code ' + response.statusCode;
callback(new Error(body)); callback(new Error(body));
return; return;
} }
Expand Down
19 changes: 18 additions & 1 deletion spec/api.spec.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -230,13 +230,14 @@ describe('new API', function() {
describe('failed api response', function() { describe('failed api response', function() {
beforeEach(function() { beforeEach(function() {
spyOn(request, 'send').andCallFake(function(url, opts, callback) { spyOn(request, 'send').andCallFake(function(url, opts, callback) {
callback(null, { statusCode: 404 }, 'Page not found'); callback(null, { statusCode: 404 }, 'page not found');
}); });
}); });


it('should trigger callback with an error', function(done) { it('should trigger callback with an error', function(done) {
api('/apps', function(e, data) { api('/apps', function(e, data) {
expect(e).toEqual(jasmine.any(Error)); expect(e).toEqual(jasmine.any(Error));
expect(e.message).toEqual('page not found');
done(); done();
}); });
}); });
Expand All @@ -247,6 +248,22 @@ describe('new API', function() {
done(); done();
}); });
}); });

describe('when no error body provided', function() {
beforeEach(function() {
request.send.andCallFake(function(uri, opts, callback) {
callback(null, { statusCode: 501 }, '');
});
});

it('should provide default error message', function(done) {
api('/apps', function(e, data) {
expect(e).toEqual(jasmine.any(Error));
expect(e.message).toMatch('server returned');
done();
});
});
});
}); });


describe('failed api data', function() { describe('failed api data', function() {
Expand Down
19 changes: 18 additions & 1 deletion spec/phonegap-build-api.spec.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -99,13 +99,14 @@ describe('phonegap-build-api', function() {
describe('failed authentication response', function() { describe('failed authentication response', function() {
beforeEach(function() { beforeEach(function() {
spyOn(request, 'post').andCallFake(function(uri, opts, callback) { spyOn(request, 'post').andCallFake(function(uri, opts, callback) {
callback(null, { statusCode: 404 }, ''); callback(null, { statusCode: 404 }, 'page not found');
}); });
}); });


it('should trigger callback with an error', function(done) { it('should trigger callback with an error', function(done) {
client.auth(options, function(e, api) { client.auth(options, function(e, api) {
expect(e).toEqual(jasmine.any(Error)); expect(e).toEqual(jasmine.any(Error));
expect(e.message).toEqual('page not found');
done(); done();
}); });
}); });
Expand All @@ -116,6 +117,22 @@ describe('phonegap-build-api', function() {
done(); done();
}); });
}); });

describe('when no error body provided', function() {
beforeEach(function() {
request.post.andCallFake(function(uri, opts, callback) {
callback(null, { statusCode: 501 }, '');
});
});

it('should provide default error message', function(done) {
client.auth(options, function(e, api) {
expect(e).toEqual(jasmine.any(Error));
expect(e.message).toMatch('server returned');
done();
});
});
});
}); });


describe('failed authentication credentials', function() { describe('failed authentication credentials', function() {
Expand Down

0 comments on commit 45e3d67

Please sign in to comment.