1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-07-31 15:24:23 +03:00

Improve MatrixError message (#2749)

This commit is contained in:
Michael Telatynski
2022-10-13 09:07:15 +01:00
committed by GitHub
parent cc025ea458
commit bbece73346
3 changed files with 15 additions and 5 deletions

View File

@ -121,7 +121,7 @@ describe("MatrixClient", function() {
}, function(error) { }, function(error) {
expect(error.httpStatus).toEqual(400); expect(error.httpStatus).toEqual(400);
expect(error.errcode).toEqual("M_SNAFU"); expect(error.errcode).toEqual("M_SNAFU");
expect(error.message).toEqual("broken"); expect(error.message).toEqual("MatrixError: [400] broken");
}); });
httpBackend!.flush(''); httpBackend!.flush('');

View File

@ -37,11 +37,17 @@ export class MatrixError extends Error {
public readonly errcode?: string; public readonly errcode?: string;
public readonly data: IErrorJson; public readonly data: IErrorJson;
constructor(errorJson: IErrorJson = {}, public httpStatus?: number) { constructor(errorJson: IErrorJson = {}, public httpStatus?: number, public url?: string) {
super(`MatrixError: ${errorJson.errcode}`); let message = errorJson.error || "Unknown message";
if (httpStatus) {
message = `[${httpStatus}] ${message}`;
}
if (url) {
message = `${message} (${url})`;
}
super(`MatrixError: ${message}`);
this.errcode = errorJson.errcode; this.errcode = errorJson.errcode;
this.name = errorJson.errcode || "Unknown error code"; this.name = errorJson.errcode || "Unknown error code";
this.message = errorJson.error || "Unknown message";
this.data = errorJson; this.data = errorJson;
} }
} }

View File

@ -80,7 +80,11 @@ export function parseErrorResponse(response: XMLHttpRequest | Response, body?: s
} }
if (contentType?.type === "application/json" && body) { if (contentType?.type === "application/json" && body) {
return new MatrixError(JSON.parse(body), response.status); return new MatrixError(
JSON.parse(body),
response.status,
isXhr(response) ? response.responseURL : response.url,
);
} }
if (contentType?.type === "text/plain") { if (contentType?.type === "text/plain") {
return new Error(`Server returned ${response.status} error: ${body}`); return new Error(`Server returned ${response.status} error: ${body}`);