-
Notifications
You must be signed in to change notification settings - Fork 19
Better error handling of non-conformant json:api responses #14
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -413,6 +413,15 @@ Transport.prototype._attachAuthToRequest = function(someRequest) { | |
| } | ||
| }; | ||
|
|
||
| Transport._defaultError = function(response) { | ||
| return { | ||
| status: "500", | ||
| code: "EUNKNOWN", | ||
| title: "An unknown error has occured", | ||
| detail: response | ||
| }; | ||
| }; | ||
|
|
||
| Transport.prototype._action = function(method, url, data, callback) { | ||
| // console.log(method, url, JSON.stringify(data, null, 2)); | ||
| var someRequest = request[method](url); | ||
|
|
@@ -435,7 +444,12 @@ Transport.prototype._action = function(method, url, data, callback) { | |
| response = JSON.parse(err.response.text); | ||
| } catch(e) { | ||
| console.error("Transport Error: " + JSON.stringify(err)); | ||
| return callback(new Error("Invalid response from server")); | ||
| return callback(Transport._defaultError(response)); | ||
| } | ||
|
|
||
| if (!(response.errors instanceof Array)) { | ||
| console.error("Invalid Error payload!", response); | ||
| return callback(Transport._defaultError(response)); | ||
| } | ||
|
|
||
| var realErrors = response.errors.map(function(apiError) { | ||
|
|
@@ -454,6 +468,10 @@ Transport.prototype._action = function(method, url, data, callback) { | |
| payload.body = JSON.parse(payload.text); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out of scope of this change, but is there any danger
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call, I'll add it. |
||
| } | ||
|
|
||
| if (!payload.body) { | ||
| return callback(Transport._defaultError(payload)); | ||
| } | ||
|
|
||
| return callback(null, payload.body, payload.body.data, payload.body.included); | ||
| }); | ||
| }; | ||
|
|
@@ -16868,6 +16886,21 @@ describe("Testing jsonapi-client", function() { | |
|
|
||
| }); | ||
|
|
||
| describe("testing invalid payloads", function() { | ||
| it("doesnt crash when we get a non-conformant response", function(done) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can actually spell
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Haha, too true! |
||
| var badClient = new Client("http://localhost:12345"); | ||
| badClient.find("articles", { }, function(err) { | ||
| assert.deepEqual(err, { | ||
| "status": "500", | ||
| "code": "EUNKNOWN", | ||
| "title": "An unknown error has occured", | ||
| "detail": undefined | ||
| }); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| }); | ||
|
|
||
| },{"../.":1,"./_testServer.js":30,"assert":6}],34:[function(require,module,exports){ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use
Array.isArrayinstead? Or is the difference largely irrelevant?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apparently
Array.isArraygoes as far back as IE9, so I guess we could?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't even think about
Array.isArrayvsinstanceof Arrayuntil now, so I'm not sure. I'll leave it to your better judgement. :)