You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-08-06 12:02:40 +03:00
exclude aborted requests and matrix errors when creating connectionerror
- throw an AbortError when aborting is detected - don't turn AbortError's into a ConnectionError - also consider the string "aborted" an AbortError for unit tests - unit tests like to reject the request with a MatrixError, so exclude those too
This commit is contained in:
@@ -3,6 +3,7 @@ import HttpBackend from "matrix-mock-request";
|
|||||||
import {MatrixClient} from "../../src/matrix";
|
import {MatrixClient} from "../../src/matrix";
|
||||||
import {MatrixScheduler} from "../../src/scheduler";
|
import {MatrixScheduler} from "../../src/scheduler";
|
||||||
import {MemoryStore} from "../../src/store/memory";
|
import {MemoryStore} from "../../src/store/memory";
|
||||||
|
import {MatrixError} from "../../src/http-api";
|
||||||
|
|
||||||
describe("MatrixClient opts", function() {
|
describe("MatrixClient opts", function() {
|
||||||
const baseUrl = "http://localhost.or.something";
|
const baseUrl = "http://localhost.or.something";
|
||||||
@@ -132,10 +133,10 @@ describe("MatrixClient opts", function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("shouldn't retry sending events", function(done) {
|
it("shouldn't retry sending events", function(done) {
|
||||||
httpBackend.when("PUT", "/txn1").fail(500, {
|
httpBackend.when("PUT", "/txn1").fail(500, new MatrixError({
|
||||||
errcode: "M_SOMETHING",
|
errcode: "M_SOMETHING",
|
||||||
error: "Ruh roh",
|
error: "Ruh roh",
|
||||||
});
|
}));
|
||||||
client.sendTextMessage("!foo:bar", "a body", "txn1").then(function(res) {
|
client.sendTextMessage("!foo:bar", "a body", "txn1").then(function(res) {
|
||||||
expect(false).toBe(true, "sendTextMessage resolved but shouldn't");
|
expect(false).toBe(true, "sendTextMessage resolved but shouldn't");
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
|
@@ -276,6 +276,9 @@ MatrixHttpApi.prototype = {
|
|||||||
callbacks.clearTimeout(xhr.timeout_timer);
|
callbacks.clearTimeout(xhr.timeout_timer);
|
||||||
var resp;
|
var resp;
|
||||||
try {
|
try {
|
||||||
|
if (xhr.status === 0) {
|
||||||
|
throw new AbortError();
|
||||||
|
}
|
||||||
if (!xhr.responseText) {
|
if (!xhr.responseText) {
|
||||||
throw new Error('No response body.');
|
throw new Error('No response body.');
|
||||||
}
|
}
|
||||||
@@ -790,10 +793,15 @@ const requestCallback = function(
|
|||||||
|
|
||||||
return function(err, response, body) {
|
return function(err, response, body) {
|
||||||
if (err) {
|
if (err) {
|
||||||
// browser-request just throws normal Error objects,
|
// the unit tests use matrix-mock-request, which throw the string "aborted" when aborting a request.
|
||||||
// not `TypeError`s like fetch does. So just assume any
|
// See https://github.com/matrix-org/matrix-mock-request/blob/3276d0263a561b5b8326b47bae720578a2c7473a/src/index.js#L48
|
||||||
// error is due to the connection.
|
const aborted = err.name === "AbortError" || err === "aborted";
|
||||||
err = new ConnectionError("request failed", err);
|
if (!aborted && !(err instanceof MatrixError)) {
|
||||||
|
// 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 {
|
||||||
@@ -930,3 +938,13 @@ export class ConnectionError extends Error {
|
|||||||
return this._cause;
|
return this._cause;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class AbortError extends Error {
|
||||||
|
constructor() {
|
||||||
|
super("Operation aborted");
|
||||||
|
}
|
||||||
|
|
||||||
|
get name() {
|
||||||
|
return "AbortError";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user