Skip to content

Commit

Permalink
Implement better messages for request errors
Browse files Browse the repository at this point in the history
While debugging some issues, we noticed that our network-related error
messages could do with more verbosity. This is what this commit does.

It also adds the HTTP status code in the message when this is the
source, of the network error. Today it is only present as a `status`
property.
  • Loading branch information
peaBerberian committed Sep 1, 2023
1 parent 0f3d147 commit 982f912
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/errors/__tests__/request_error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,19 @@ describe("errors - RequestError", () => {
expect(requestError.xhr).toBe(xhr);
expect(requestError.status).toBe(355);
expect(requestError.type).toBe("ERROR_EVENT");
expect(requestError.message).toBe("ERROR_EVENT");
expect(requestError.message).toBe(
"An error prevented the request to be performed successfully"
);
xhr.abort();
});
it("should authorize no XHR", () => {
const requestError = new RequestError("foo", 355, "ERROR_EVENT");
const requestError = new RequestError("foo", 355, "TIMEOUT");
expect(requestError).toBeInstanceOf(Error);
expect(requestError.name).toBe("RequestError");
expect(requestError.url).toBe("foo");
expect(requestError.xhr).toBe(undefined);
expect(requestError.status).toBe(355);
expect(requestError.type).toBe("ERROR_EVENT");
expect(requestError.message).toBe("ERROR_EVENT");
expect(requestError.type).toBe("TIMEOUT");
expect(requestError.message).toBe("The request timeouted");
});
});
17 changes: 16 additions & 1 deletion src/errors/request_error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ export default class RequestError extends Error {
}
this.status = status;
this.type = type;
this.message = type;

switch (type) {
case "TIMEOUT":
this.message = "The request timeouted";
break;
case "ERROR_EVENT":
this.message = "An error prevented the request to be performed successfully";
break;
case "PARSE_ERROR":
this.message = "An error happened while formatting the response data";
break;
case "ERROR_HTTP_CODE":
this.message = "An HTTP status code indicating failure was received: " +
String(this.status);
break;
}
}
}

0 comments on commit 982f912

Please sign in to comment.