From f17cd142d571c56e0b0c16042bc6d7c161426273 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Thu, 20 May 2021 15:43:22 +0100 Subject: [PATCH 1/2] Fix uploadContent not rejecting promise when http status code >= 400 --- src/http-api.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/http-api.js b/src/http-api.js index 26b3d5f5b..1a90b0045 100644 --- a/src/http-api.js +++ b/src/http-api.js @@ -790,6 +790,8 @@ const requestCallback = function( userDefinedCallback = userDefinedCallback || function() {}; return function(err, response, body) { + const httpStatus = response.status || response.statusCode; // XMLHttpRequest vs http.IncomingMessage + if (err) { // the unit tests use matrix-mock-request, which throw the string "aborted" when aborting a request. // See https://github.com/matrix-org/matrix-mock-request/blob/3276d0263a561b5b8326b47bae720578a2c7473a/src/index.js#L48 @@ -803,7 +805,7 @@ const requestCallback = function( } if (!err) { try { - if (response.statusCode >= 400) { + if (httpStatus >= 400) { err = parseErrorResponse(response, body); } else if (bodyParser) { body = bodyParser(body); @@ -818,7 +820,7 @@ const requestCallback = function( userDefinedCallback(err); } else { const res = { - code: response.statusCode, + code: httpStatus, // XXX: why do we bother with this? it doesn't work for // XMLHttpRequest, so clearly we don't use it. @@ -842,7 +844,7 @@ const requestCallback = function( * @returns {Error} */ function parseErrorResponse(response, body) { - const httpStatus = response.statusCode; + const httpStatus = response.status || response.statusCode; // XMLHttpRequest vs http.IncomingMessage const contentType = getResponseContentType(response); let err; From e8367ad24168eb237f85f61dc7884e7883aa4ab9 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Thu, 20 May 2021 18:33:17 +0100 Subject: [PATCH 2/2] fix httpStatus being resolved when response is undefined --- src/http-api.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/http-api.js b/src/http-api.js index 1a90b0045..dfa0c8c57 100644 --- a/src/http-api.js +++ b/src/http-api.js @@ -790,8 +790,6 @@ const requestCallback = function( userDefinedCallback = userDefinedCallback || function() {}; return function(err, response, body) { - const httpStatus = response.status || response.statusCode; // XMLHttpRequest vs http.IncomingMessage - if (err) { // the unit tests use matrix-mock-request, which throw the string "aborted" when aborting a request. // See https://github.com/matrix-org/matrix-mock-request/blob/3276d0263a561b5b8326b47bae720578a2c7473a/src/index.js#L48 @@ -805,6 +803,7 @@ const requestCallback = function( } if (!err) { try { + const httpStatus = response.status || response.statusCode; // XMLHttpRequest vs http.IncomingMessage if (httpStatus >= 400) { err = parseErrorResponse(response, body); } else if (bodyParser) { @@ -820,7 +819,7 @@ const requestCallback = function( userDefinedCallback(err); } else { const res = { - code: httpStatus, + code: response.status || response.statusCode, // XMLHttpRequest vs http.IncomingMessage // XXX: why do we bother with this? it doesn't work for // XMLHttpRequest, so clearly we don't use it.