Skip to content

Commit

Permalink
♻️ Throw a detailed error when requests fail
Browse files Browse the repository at this point in the history
  • Loading branch information
toridoriv committed Oct 2, 2023
1 parent f4e17b4 commit 6bf2130
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 6 additions & 0 deletions server/@errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ export class Unauthorized extends BaseError {
super(detail, "4002", source);
}
}

export class UnknownFetchError extends BaseError {
constructor(detail: string, readonly source?: Record<string, string>) {
super(detail, "5000", source);
}
}
10 changes: 9 additions & 1 deletion server/@utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { LOCATION } from "@constants";
import { UnknownFetchError } from "@errors";

/* -------------------------------------------------------------------------- */
/* Internal Constants and Classes */
Expand All @@ -17,7 +18,14 @@ export async function fetchFromApi<T>(
const response = await fetch(LOCATION + path, config);

if (!response.ok) {
throw new Error("idk");
throw new UnknownFetchError(
`There was a problem making a request to ${LOCATION + path}`,
{
status: String(response.status),
statusText: response.statusText,
body: await response.text(),
},
);
}

return response.json();
Expand Down

0 comments on commit 6bf2130

Please sign in to comment.