diff --git a/src/http-api.js b/src/http-api.js index 49c3b0929..99a889eb6 100644 --- a/src/http-api.js +++ b/src/http-api.js @@ -789,6 +789,12 @@ const requestCallback = function( userDefinedCallback = userDefinedCallback || function() {}; return function(err, response, body) { + if (err) { + // browser-request just throws normal Error objects, + // not `TypeError`s like fetch does. So just assume any + // error is due to the connection. + err = new ConnectionError("request failed", err); + } if (!err) { try { if (response.statusCode >= 400) { @@ -902,3 +908,25 @@ export class MatrixError extends Error { this.data = errorJson; } } + +/** + * Construct a ConnectionError. This is a JavaScript Error indicating + * that a request failed because of some error with the connection, either + * CORS was not correctly configured on the server, the server didn't response, + * the request timed out, or the internet connection on the client side went down. + * @constructor + */ +export class ConnectionError extends Error { + constructor(message, cause = undefined) { + super(message + (cause ? `: ${cause.message}` : "")); + this._cause = cause; + } + + get name() { + return "ConnectionError"; + } + + get cause() { + return this._cause; + } +}