Improve error handling
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
export type DeepPartial<T> = T extends object
|
||||
? {
|
||||
[P in keyof T]?: DeepPartial<T[P]>;
|
||||
}
|
||||
[P in keyof T]?: DeepPartial<T[P]>;
|
||||
}
|
||||
: T;
|
||||
|
||||
export const formatSlug = (name: string): string => {
|
||||
@@ -13,25 +13,3 @@ export const formatSlug = (name: string): string => {
|
||||
.replace(/--+/g, "-") // collapse multiple dashes
|
||||
.replace(/^([^a-z0-9_])/, "_$1"); // if it starts with an invalid char, prefix underscore
|
||||
};
|
||||
|
||||
export class FetchError extends Error {
|
||||
public payload: Object | undefined;
|
||||
|
||||
constructor(
|
||||
message: string,
|
||||
public response: Response,
|
||||
) {
|
||||
super(message);
|
||||
this.name = "FetchError";
|
||||
}
|
||||
|
||||
async parse() {
|
||||
try {
|
||||
this.payload = (await this.response.clone().json()) as Object;
|
||||
} catch {
|
||||
this.payload = await this.response.text().catch(() => undefined);
|
||||
} finally {
|
||||
return this.payload;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user