feat: add better error handling (#605)

* feat: add better error handling

* refactor: update after review

* refactor: remove unnecessary code

* refactor: better path structure

* refactor: fetch-wrapper after code review

Co-authored-by: Mihai Colceriu <[email protected]>
This commit is contained in:
Mihai CandMihai Colceriu authored and GitHub committed 2021-09-09 16:51:06 +03:00
1 parent 70f595ec08
commit 903f7729c7
18 files changed
+410 -228

No files matched your search

+33
View File
@@ -0,0 +1,33 @@
export class HttpError<TCode extends number = number> extends Error {
public readonly cause: unknown;
public readonly statusCode: TCode;
public readonly message: string;
public readonly url: string | undefined;
public readonly method: string | undefined;
constructor(opts: { url?: string; method?: string; message?: string; statusCode: TCode; cause?: unknown }) {
super(opts.message ?? `HTTP Error ${opts.statusCode} `);
Object.setPrototypeOf(this, HttpError.prototype);
this.name = HttpError.prototype.constructor.name;
this.cause = opts.cause;
this.statusCode = opts.statusCode;
this.url = opts.url;
this.method = opts.method;
this.message = opts.message ?? `HTTP Error ${opts.statusCode}`;
if (opts.cause instanceof Error && opts.cause.stack) {
this.stack = opts.cause.stack;
}
}
public static fromRequest(request: Request, response: Response) {
return new HttpError({
message: response.statusText,
url: response.url,
method: request.method,
statusCode: response.status,
});
}
}
+2
View File
@@ -0,0 +1,2 @@
// Base http Error
export { HttpError } from "./http-error";