From 6bf213086d9efc751c0c290915e4b17188619edc Mon Sep 17 00:00:00 2001 From: toridoriv Date: Mon, 2 Oct 2023 11:44:49 -0300 Subject: [PATCH] :recycle: Throw a detailed error when requests fail --- server/@errors.ts | 6 ++++++ server/@utils.ts | 10 +++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/server/@errors.ts b/server/@errors.ts index 6e560a1..3cd3e7c 100644 --- a/server/@errors.ts +++ b/server/@errors.ts @@ -28,3 +28,9 @@ export class Unauthorized extends BaseError { super(detail, "4002", source); } } + +export class UnknownFetchError extends BaseError { + constructor(detail: string, readonly source?: Record) { + super(detail, "5000", source); + } +} diff --git a/server/@utils.ts b/server/@utils.ts index f491726..9df66e3 100644 --- a/server/@utils.ts +++ b/server/@utils.ts @@ -1,4 +1,5 @@ import { LOCATION } from "@constants"; +import { UnknownFetchError } from "@errors"; /* -------------------------------------------------------------------------- */ /* Internal Constants and Classes */ @@ -17,7 +18,14 @@ export async function fetchFromApi( 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();