Skip to content
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

Include statusText in error object #511

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
1 change: 1 addition & 0 deletions shared/fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ Fetcher.prototype.fetchFromApi = function(spec, options, callback) {
respOutput = JSON.stringify(resp);
err = new Error("ERROR fetching model '" + fetcher.modelUtils.modelName(model.constructor) + "' with options '" + JSON.stringify(options) + "'. Response: " + respOutput);
err.status = resp.status;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say to use underscore and a whitelist instead, this way we can easily add or remove things from the whitelist.

_.extend(err, _.pick(resp, [
  'status', 'statusText'
]));

err.statusText = resp.statusText;
err.body = body;
callback(err);
}
Expand Down
3 changes: 2 additions & 1 deletion shared/syncer.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ function clientSync(method, model, options) {
}
resp = {
body: body,
status: xhr.status
status: xhr.status,
statusText: xhr.statusText
};
error(resp);
}
Expand Down
14 changes: 10 additions & 4 deletions test/shared/syncer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ describe('syncer', function() {
fakeXhr = {
responseText: '{"foo": "bar"}',
status: 418,
statusText: 0,
getResponseHeader: sinon.stub()
};
options.error = syncErrorHandler;
Expand All @@ -264,7 +265,8 @@ describe('syncer', function() {
it('should call the original error handler with status and body', function () {
var expectedResponse = {
body: fakeXhr.responseText,
status: fakeXhr.status
status: fakeXhr.status,
statusText: fakeXhr.statusText
};

syncer.clientSync.call(model, 'read', model, options);
Expand All @@ -276,7 +278,8 @@ describe('syncer', function() {
it('should parse the payload if content-type is "application/json"', function () {
var expectedResponse = {
body: JSON.parse(fakeXhr.responseText),
status: fakeXhr.status
status: fakeXhr.status,
statusText: fakeXhr.statusText,
};

fakeXhr.getResponseHeader.withArgs('content-type').returns('application/json');
Expand Down Expand Up @@ -334,6 +337,7 @@ describe('syncer', function() {
fakeXhr = {
responseText: '{"foo": "bar"}',
status: 418,
statusText: 0,
getResponseHeader: sinon.stub()
};
options.error = syncErrorHandler;
Expand All @@ -343,7 +347,8 @@ describe('syncer', function() {
it('should call the original error handler with status and body', function () {
var expectedResponse = {
body: fakeXhr.responseText,
status: fakeXhr.status
status: fakeXhr.status,
statusText: fakeXhr.statusText
};

syncer.clientSync.call(model, 'read', model, options);
Expand All @@ -355,7 +360,8 @@ describe('syncer', function() {
it('should parse the payload if content-type is "application/json"', function () {
var expectedResponse = {
body: JSON.parse(fakeXhr.responseText),
status: fakeXhr.status
status: fakeXhr.status,
statusText: fakeXhr.statusText
};

fakeXhr.getResponseHeader.withArgs('content-type').returns('application/json');
Expand Down