Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spec/integ/crypto/verification.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1347,7 +1347,7 @@ describe("verification", () => {
await sendBackupGossipAndExpectVersion(
requestId!,
BACKUP_DECRYPTION_KEY_BASE64,
new MatrixError({ errcode: "M_NOT_FOUND", error: "No backup found" }, 404),
new MatrixError({ errcode: "M_NOT_FOUND", error: "No backup found" }, { httpStatus: 404 }),
);

// the backup secret should not be cached
Expand Down
13 changes: 7 additions & 6 deletions spec/integ/matrix-client-methods.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ describe("MatrixClient", function () {
},
function (error) {
expect(error.httpStatus).toEqual(400);
expect(error.method).toEqual("POST");
expect(error.errcode).toEqual("M_SNAFU");
expect(error.message).toEqual("MatrixError: [400] broken");
expect(error.message).toEqual("MatrixError: [POST 400] broken");
},
);

Expand Down Expand Up @@ -380,12 +381,12 @@ describe("MatrixClient", function () {
[
403,
{ errcode: "M_FORBIDDEN", error: "You don't have permission to knock" },
"[M_FORBIDDEN: MatrixError: [403] You don't have permission to knock]",
"[M_FORBIDDEN: MatrixError: [POST 403] You don't have permission to knock]",
],
[
500,
{ errcode: "INTERNAL_SERVER_ERROR" },
"[INTERNAL_SERVER_ERROR: MatrixError: [500] Unknown message]",
"[INTERNAL_SERVER_ERROR: MatrixError: [POST 500] Unknown message]",
],
];

Expand Down Expand Up @@ -1556,7 +1557,7 @@ describe("MatrixClient", function () {

const prom = client.setPowerLevel("!room_id:server", userId, 42);
await Promise.all([
expect(prom).rejects.toMatchInlineSnapshot(`[ERR_DERP: MatrixError: [500] Unknown message]`),
expect(prom).rejects.toMatchInlineSnapshot(`[ERR_DERP: MatrixError: [GET 500] Unknown message]`),
httpBackend.flushAllExpected(),
]);
});
Expand Down Expand Up @@ -1801,7 +1802,7 @@ describe("MatrixClient", function () {
function (error) {
expect(error.httpStatus).toEqual(errorUnrecogStatus);
expect(error.errcode).toEqual(errorUnrecogBody.errcode);
expect(error.message).toEqual(`MatrixError: [${errorUnrecogStatus}] ${errorUnrecogBody.error}`);
expect(error.message).toEqual(`MatrixError: [GET ${errorUnrecogStatus}] ${errorUnrecogBody.error}`);
},
);

Expand All @@ -1819,7 +1820,7 @@ describe("MatrixClient", function () {
function (error) {
expect(error.httpStatus).toEqual(errorBadreqStatus);
expect(error.errcode).toEqual(errorBadreqBody.errcode);
expect(error.message).toEqual(`MatrixError: [${errorBadreqStatus}] ${errorBadreqBody.error}`);
expect(error.message).toEqual(`MatrixError: [GET ${errorBadreqStatus}] ${errorBadreqBody.error}`);
},
);

Expand Down
15 changes: 9 additions & 6 deletions spec/integ/matrix-client-opts.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import HttpBackend from "matrix-mock-request";

import * as utils from "../test-utils/test-utils";
import { ClientEvent, MatrixClient } from "../../src/matrix";
import { ClientEvent, MatrixClient, Method } from "../../src/matrix";
import { MatrixScheduler } from "../../src/scheduler";
import { MemoryStore } from "../../src/store/memory";
import { MatrixError } from "../../src/http-api";
Expand Down Expand Up @@ -151,15 +151,18 @@ describe("MatrixClient opts", function () {
it("shouldn't retry sending events", async () => {
httpBackend.when("PUT", "/txn1").respond(
500,
new MatrixError({
errcode: "M_SOMETHING",
error: "Ruh roh",
}),
new MatrixError(
{
errcode: "M_SOMETHING",
error: "Ruh roh",
},
{ httpStatus: 500, method: Method.Put },
),
);

await expect(
Promise.all([client.sendTextMessage("!foo:bar", "a body", "txn1"), httpBackend.flush("/txn1", 1)]),
).rejects.toThrow("MatrixError: [500] Ruh roh");
).rejects.toThrow("MatrixError: [PUT 500] Ruh roh");
});

it("shouldn't queue events", async () => {
Expand Down
15 changes: 9 additions & 6 deletions spec/integ/matrix-client-room-timeline.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -799,12 +799,15 @@ describe("MatrixClient room timelines", function () {
})
.respond(
500,
new MatrixError({
errcode: "TEST_FAKE_ERROR",
error:
"We purposely intercepted this /context request to make it fail " +
"in order to test whether the refresh timeline code is resilient",
}),
new MatrixError(
{
errcode: "TEST_FAKE_ERROR",
error:
"We purposely intercepted this /context request to make it fail " +
"in order to test whether the refresh timeline code is resilient",
},
{ httpStatus: 500 },
),
);

// Refresh the timeline and expect it to fail
Expand Down
10 changes: 7 additions & 3 deletions spec/integ/rendezvous/MSC4108SignInWithQR.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ describe("MSC4108SignInWithQR", () => {
it("should be able to connect with opponent and share verificationUri", async () => {
await Promise.all([ourLogin.negotiateProtocols(), opponentLogin.negotiateProtocols()]);

mocked(client.getDevice).mockRejectedValue(new MatrixError({ errcode: "M_NOT_FOUND" }, 404));
mocked(client.getDevice).mockRejectedValue(
new MatrixError({ errcode: "M_NOT_FOUND" }, { httpStatus: 404 }),
);

await Promise.all([
expect(ourLogin.deviceAuthorizationGrant()).resolves.toEqual({
Expand Down Expand Up @@ -280,7 +282,9 @@ describe("MSC4108SignInWithQR", () => {
await opponentLogin.send({
type: PayloadType.Success,
});
mocked(client.getDevice).mockRejectedValue(new MatrixError({ errcode: "M_NOT_FOUND" }, 404));
mocked(client.getDevice).mockRejectedValue(
new MatrixError({ errcode: "M_NOT_FOUND" }, { httpStatus: 404 }),
);

const ourProm = ourLogin.shareSecrets();
await expect(ourProm).rejects.toThrow("New device not found");
Expand All @@ -298,7 +302,7 @@ describe("MSC4108SignInWithQR", () => {
type: PayloadType.Success,
});
mocked(client.getDevice).mockRejectedValue(
new MatrixError({ errcode: "M_UNKNOWN", error: "The message" }, 500),
new MatrixError({ errcode: "M_UNKNOWN", error: "The message" }, { httpStatus: 500 }),
);

await expect(ourLogin.shareSecrets()).rejects.toThrow("The message");
Expand Down
11 changes: 7 additions & 4 deletions spec/integ/sliding-sync-sdk.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,10 +590,13 @@ describe("SlidingSyncSdk", () => {
SlidingSyncEvent.Lifecycle,
SlidingSyncState.RequestFinished,
null,
new MatrixError({
errcode: "M_UNKNOWN_TOKEN",
message: "Oh no your access token is no longer valid",
}),
new MatrixError(
{
errcode: "M_UNKNOWN_TOKEN",
message: "Oh no your access token is no longer valid",
},
{ httpStatus: 401 },
),
);
expect(sdk!.getSyncState()).toEqual(SyncState.Error);
expect(mockSlidingSync!.stop).toHaveBeenCalled();
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/embedded.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ describe("RoomWidgetClient", () => {
response: errorData,
},
});
const matrixError = new MatrixError(errorData, errorStatusCode, errorUrl);
const matrixError = new MatrixError(errorData, { httpStatus: errorStatusCode, url: errorUrl });

widgetApi.transport.send.mockRejectedValue(widgetError);

Expand Down Expand Up @@ -994,7 +994,7 @@ describe("RoomWidgetClient", () => {
response: errorData,
},
});
const matrixError = new MatrixError(errorData, errorStatusCode, errorUrl);
const matrixError = new MatrixError(errorData, { httpStatus: errorStatusCode, url: errorUrl });

widgetApi.transport.sendComplete.mockRejectedValue(widgetError);

Expand Down
4 changes: 2 additions & 2 deletions spec/unit/http-api/errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ describe("MatrixError", () => {
headers = new Headers({ "Content-Type": "application/json" });
});

function makeMatrixError(httpStatus: number, data: IErrorJson, url?: string): MatrixError {
return new MatrixError(data, httpStatus, url, undefined, headers);
function makeMatrixError(httpStatus: number, data: IErrorJson, url = "https://default-url"): MatrixError {
return new MatrixError(data, { httpStatus, url, httpHeaders: headers });
}

it("should accept absent retry time from rate-limit error", () => {
Expand Down
7 changes: 5 additions & 2 deletions spec/unit/http-api/fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ describe("FetchHttpApi", () => {
const baseUrl = "http://baseUrl";
const idBaseUrl = "http://idBaseUrl";
const prefix = ClientPrefix.V3;
const tokenInactiveError = new MatrixError({ errcode: "M_UNKNOWN_TOKEN", error: "Token is not active" }, 401);
const tokenInactiveError = new MatrixError(
{ errcode: "M_UNKNOWN_TOKEN", error: "Token is not active" },
{ httpStatus: 401 },
);

beforeEach(() => {
jest.useRealTimers();
Expand Down Expand Up @@ -322,7 +325,7 @@ describe("FetchHttpApi", () => {
error: "Token is not active",
soft_logout: false,
};
const unknownTokenErr = new MatrixError(unknownTokenErrBody, 401);
const unknownTokenErr = new MatrixError(unknownTokenErrBody, { httpStatus: 401, method: Method.Post });
const unknownTokenResponse = {
ok: false,
status: 401,
Expand Down
50 changes: 34 additions & 16 deletions spec/unit/http-api/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
MatrixError,
MatrixSafetyError,
MatrixSafetyErrorCode,
Method,
parseErrorResponse,
retryNetworkOperation,
timeoutSignal,
Expand Down Expand Up @@ -112,14 +113,15 @@ describe("parseErrorResponse", () => {
...xhrHeaderMethods,
status: 500,
} as XMLHttpRequest,
Method.Post,
'{"errcode": "TEST"}',
),
).toStrictEqual(
new MatrixError(
{
errcode: "TEST",
},
500,
{ httpStatus: 500, method: Method.Post },
),
);
});
Expand All @@ -132,14 +134,15 @@ describe("parseErrorResponse", () => {
headers,
status: 500,
} as Response,
Method.Post,
'{"errcode": "TEST"}',
),
).toStrictEqual(
new MatrixError(
{
errcode: "TEST",
},
500,
{ httpStatus: 500, method: Method.Post },
),
);
});
Expand All @@ -153,15 +156,15 @@ describe("parseErrorResponse", () => {
...xhrHeaderMethods,
status: 500,
} as XMLHttpRequest,
Method.Post,
'{"errcode": "TEST"}',
),
).toStrictEqual(
new MatrixError(
{
errcode: "TEST",
},
500,
"https://example.com",
{ httpStatus: 500, url: "https://example.com", method: Method.Post },
),
);
});
Expand All @@ -175,15 +178,15 @@ describe("parseErrorResponse", () => {
headers,
status: 500,
} as Response,
Method.Post,
'{"errcode": "TEST"}',
),
).toStrictEqual(
new MatrixError(
{
errcode: "TEST",
},
500,
"https://example.com",
{ httpStatus: 500, url: "https://example.com", method: Method.Post },
),
);
});
Expand All @@ -210,6 +213,7 @@ describe("parseErrorResponse", () => {
headers,
status: 400,
} as Response,
Method.Post,
JSON.stringify(errContent),
) as MatrixSafetyError;
expect(value).toBeInstanceOf(MatrixSafetyError);
Expand All @@ -233,20 +237,26 @@ describe("parseErrorResponse", () => {
it("should resolve HTTP Errors from XHR with headers", () => {
headers.set("Content-Type", "text/plain");
addHeaders(headers);
const err = parseErrorResponse({
...xhrHeaderMethods,
status: 500,
} as XMLHttpRequest) as HTTPError;
const err = parseErrorResponse(
{
...xhrHeaderMethods,
status: 500,
} as XMLHttpRequest,
Method.Post,
) as HTTPError;
compareHeaders(headers, err.httpHeaders);
});

it("should resolve HTTP Errors from fetch with headers", () => {
headers.set("Content-Type", "text/plain");
addHeaders(headers);
const err = parseErrorResponse({
headers,
status: 500,
} as Response) as HTTPError;
const err = parseErrorResponse(
{
headers,
status: 500,
} as Response,
Method.Post,
) as HTTPError;
compareHeaders(headers, err.httpHeaders);
});

Expand All @@ -258,6 +268,7 @@ describe("parseErrorResponse", () => {
...xhrHeaderMethods,
status: 500,
} as XMLHttpRequest,
Method.Post,
'{"errcode": "TEST"}',
) as MatrixError;
compareHeaders(headers, err.httpHeaders);
Expand All @@ -271,6 +282,7 @@ describe("parseErrorResponse", () => {
headers,
status: 500,
} as Response,
Method.Post,
'{"errcode": "TEST"}',
) as MatrixError;
compareHeaders(headers, err.httpHeaders);
Expand All @@ -294,9 +306,10 @@ describe("parseErrorResponse", () => {
headers,
status: 500,
} as Response,
Method.Post,
'{"errcode": "TEST"}',
),
).toStrictEqual(new HTTPError("Server returned 500 error", 500));
).toStrictEqual(new HTTPError("Server returned 500 error", { httpStatus: 500, method: Method.Post }));
});

it("should handle empty type gracefully", () => {
Expand All @@ -307,6 +320,7 @@ describe("parseErrorResponse", () => {
headers,
status: 500,
} as Response,
Method.Post,
'{"errcode": "TEST"}',
),
).toStrictEqual(new Error("Error parsing Content-Type '': TypeError: argument string is required"));
Expand All @@ -320,6 +334,7 @@ describe("parseErrorResponse", () => {
headers,
status: 500,
} as Response,
Method.Post,
'{"errcode": "TEST"}',
),
).toStrictEqual(new Error("Error parsing Content-Type 'unknown': TypeError: invalid media type"));
Expand All @@ -333,9 +348,12 @@ describe("parseErrorResponse", () => {
headers,
status: 418,
} as Response,
Method.Post,
"I'm a teapot",
),
).toStrictEqual(new HTTPError("Server returned 418 error: I'm a teapot", 418));
).toStrictEqual(
new HTTPError("Server returned 418 error: I'm a teapot", { httpStatus: 418, method: Method.Post }),
);
});
});

Expand Down
Loading
Loading