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";
+58
View File
@@ -0,0 +1,58 @@
import { HttpError } from "@lib/core/http/error";
async function http<T>(path: string, config: RequestInit): Promise<T> {
const request = new Request(path, config);
const response: Response = await fetch(request);
if (!response.ok) {
const err = HttpError.fromRequest(request, response);
throw err;
}
// may error if there is no body, return empty array
return await response.json();
}
export async function get<T>(path: string, config?: RequestInit): Promise<T> {
const init = { method: "GET", ...config };
return await http<T>(path, init);
}
export async function post<T, U>(path: string, body: T, config?: RequestInit): Promise<U> {
const init = {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
...config,
};
return await http<U>(path, init);
}
export async function put<T, U>(path: string, body: T, config?: RequestInit): Promise<U> {
const init = {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
...config,
};
return await http<U>(path, init);
}
export async function patch<T, U>(path: string, body: T, config?: RequestInit): Promise<U> {
const init = {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
...config,
};
return await http<U>(path, init);
}
export async function remove<T, U>(path: string, body: T, config?: RequestInit): Promise<U> {
const init = {
method: "DELETE",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
...config,
};
return await http<U>(path, init);
}