Skip to content

Commit e97956a

Browse files
committed
Correctly parse superagent errors
1 parent a0ec0a8 commit e97956a

File tree

7 files changed

+21
-6
lines changed

7 files changed

+21
-6
lines changed

lib/graph-js-sdk-web.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ class ResponseHandler {
323323
static ParseError(rawErr) {
324324
let errObj;
325325
if (!('rawResponse' in rawErr)) {
326-
if (rawErr.response !== undefined && 'body' in rawErr.response && 'error' in rawErr.response.body) {
326+
if (rawErr.response !== undefined && rawErr.response.body !== null && 'error' in rawErr.response.body) {
327327
errObj = rawErr.response.body.error;
328328
}
329329
}
@@ -1935,7 +1935,7 @@ Emitter.prototype.hasListeners = function(event){
19351935
},{}],11:[function(require,module,exports){
19361936
module.exports={
19371937
"name": "msgraph-sdk-javascript",
1938-
"version": "0.1.4",
1938+
"version": "0.2.0",
19391939
"description": "Microsoft Graph JavaScript SDK",
19401940
"main": "lib/src/index.js",
19411941
"typings": "lib/src/index",

lib/src/ResponseHandler.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/ResponseHandler.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/test/responseHandling.js

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/test/responseHandling.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResponseHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class ResponseHandler {
3131
let errObj; // path to object containing innerError (see above schema)
3232

3333
if (!('rawResponse' in rawErr)) { // if superagent correctly parsed the JSON
34-
if (rawErr.response !== undefined && 'body' in rawErr.response && 'error' in rawErr.response.body) { // some 404s don't return an error object
34+
if (rawErr.response !== undefined && rawErr.response.body !== null && 'error' in rawErr.response.body) { // some 404s don't return an error object
3535
errObj = rawErr.response.body.error;
3636
}
3737
} else {

test/responseHandling.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,14 @@ describe('#ParseError()', function() {
6060
assert.equal(err.requestId, _500_SUPERAGENT_RES.response.body.error.innerError["request-id"]);
6161
});
6262
});
63+
64+
65+
it('parses a 404 superagent error', function() {
66+
let rawErr = JSON.parse('{"original":null,"response":{"req":{"method":"GET","url":"https://graph.microsoft.com/v1.0/users/4470c514-8ac5-4f2d-8116-870d2c41bdf6/photo/$value","headers":{"authorization":"Bearer abc","cache-control":"no-cache","sdkversion":"graph-js-0.2.0"}},"xhr":{},"text":null,"statusText":"Not Found","statusCode":404,"status":404,"statusType":4,"info":false,"ok":false,"clientError":true,"serverError":false,"error":{"status":404,"method":"GET","url":"https://graph.microsoft.com/v1.0/users/4470c514-8ac5-4f2d-8116-870d2c41bdf6/photo/$value"},"accepted":false,"noContent":false,"badRequest":false,"unauthorized":false,"notAcceptable":false,"notFound":true,"forbidden":false,"headers":{"client-request-id":"3726fdf7-8ae6-47c0-9f6a-5847982738d2","content-type":"text/plain","cache-control":"private","request-id":"3726fdf7-8ae6-47c0-9f6a-5847982738d2"},"header":{"client-request-id":"3726fdf7-8ae6-47c0-9f6a-5847982738d2","content-type":"text/plain","cache-control":"private","request-id":"3726fdf7-8ae6-47c0-9f6a-5847982738d2"},"type":"text/plain","body":null},"status":404}');
67+
68+
let graphErr = ResponseHandler.ParseError(rawErr);
69+
70+
assert.equal(graphErr.statusCode, 404);
71+
72+
})
6373
});

0 commit comments

Comments
 (0)