Skip to content

Commit

Permalink
@thunderstore/thunderstore-api: improve error handling
Browse files Browse the repository at this point in the history
Sentry handling doesn't need to be done on package level since we're
doing it on app level. Try to throw useful error messages, taking into
account I don't how much of the error stack is available on Sentry or
NextJs logs this way.

Refs no ticket
  • Loading branch information
anttimaki committed Nov 10, 2023
1 parent 54a4416 commit 888091a
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions packages/thunderstore-api/src/apiFetch.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { RequestConfig } from "./index";

/**
* TODO: plug Sentry to error handling.
*/
export async function apiFetch(
config: RequestConfig,
path: string,
Expand All @@ -16,19 +13,23 @@ export async function apiFetch(
try {
response = await fetch(url, settings);
} catch (e) {
throw new Error(e instanceof Error ? e.message : "Data fetching error");
const msg = e instanceof Error ? e.message : "Unknown error";
throw new Error(`Fetching ${url.href}: ${msg}`);
}

if (!response.ok) {
throw new Error(`${response.status}: ${response.statusText}`);
const prefix = `Fetching ${url.href}`;
throw new Error(`${prefix}: (${response.status}) ${response.statusText}`);
}

let values;

try {
values = await response.json();
} catch (e) {
throw new Error(e instanceof Error ? e.message : "Deserialization error");
const prefix = `Deserializing response from ${url.href}`;
const msg = e instanceof Error ? e.message : "Unknown error";
throw new Error(`${prefix}: ${msg}`);
}

return values;
Expand Down

0 comments on commit 888091a

Please sign in to comment.