Skip to content
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2.7.2
=====

* (improvement) Also pass response in `ApiError`.


2.7.1
=====

Expand Down
20 changes: 20 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,42 @@ type ApiFetchSettings = Readonly<{

class ApiError extends Error
{
readonly #response: Response;
readonly #errorCode: string;
readonly #data: unknown;
readonly #errorMessage: string|null;


constructor (
response: Response,
errorCode: string,
data: unknown = undefined,
errorMessage: string|null|undefined = undefined,
)
{
super(`The API request failed due to an error: ${errorCode}`);
this.#response = response;
this.#errorCode = errorCode;
this.#data = data;
this.#errorMessage = errorMessage ?? null;
}

/**
*
*/
get is404 ()
{
return 404 === this.statusCode;
}

/**
*
*/
get statusCode () : number
{
return this.#response.status;
}

/**
*
*/
Expand Down Expand Up @@ -269,6 +288,7 @@ export async function fetchApi <
}

throw new ApiError(
response,
failureResponse.data.error,
failureResponse.data.data,
failureResponse.data.errorMessage,
Expand Down