Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Commit

Permalink
fix(error-handling): correcão do error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
andrefelipeschulle committed Jul 4, 2024
1 parent ece6edb commit 6aae191
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@brainylab/fetch-wrapper",
"version": "0.5.1",
"version": "0.5.2",
"keywords": [
"fetch",
"wrapper",
Expand Down
2 changes: 2 additions & 0 deletions src/errors/http-request-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type DataRequestErrors = {
export class HttpRequestError extends Error {
status: number;
message: string;
data?: unknown;

constructor(response: Response, data?: DataRequestErrors) {
const { status, statusText } = response;
Expand All @@ -14,5 +15,6 @@ export class HttpRequestError extends Error {
this.name = 'HttpRequestError';
this.status = status;
this.message = data?.message || statusText;
this.data = data;
}
}
2 changes: 2 additions & 0 deletions src/lib/error-handling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type FetchTypeError = TypeError & {
export type ErrorHandlingResponse = {
status?: number;
message: string;
data?: unknown;
error: 'HTTP_REQUEST_ERROR' | 'CONNECTION_REFUSED' | 'UNEXPECTED_ERROR';
throw?: unknown;
};
Expand All @@ -16,6 +17,7 @@ export function fwprErrorHandling(error: unknown): ErrorHandlingResponse {
return {
status: error.status,
message: error.message,
data: error.data,
error: 'HTTP_REQUEST_ERROR',
};
}
Expand Down
4 changes: 1 addition & 3 deletions src/lib/fetch-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ export class FetchWrapper implements FetchMethods {
await this.interceptors.response.error(this.response, this.data);
}

throw new HttpRequestError(this.response as Response, {
message: this.data,
});
throw new HttpRequestError(this.response as Response, this.data);
}

const data = await this.response.json();
Expand Down

0 comments on commit 6aae191

Please sign in to comment.