You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-25 05:23:13 +03:00
throw ConnectionError when request fails
This commit is contained in:
@@ -789,6 +789,12 @@ const requestCallback = function(
|
|||||||
userDefinedCallback = userDefinedCallback || function() {};
|
userDefinedCallback = userDefinedCallback || function() {};
|
||||||
|
|
||||||
return function(err, response, body) {
|
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) {
|
if (!err) {
|
||||||
try {
|
try {
|
||||||
if (response.statusCode >= 400) {
|
if (response.statusCode >= 400) {
|
||||||
@@ -902,3 +908,25 @@ export class MatrixError extends Error {
|
|||||||
this.data = errorJson;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user