Skip to content

Commit

Permalink
fix: better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
stepan662 committed Apr 16, 2024
1 parent 77a60a1 commit f5702b6
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions packages/web/src/package/ui/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import { paths } from './apiSchema.generated';
import { GlobalOptions } from './QueryProvider';
import { RequestParamsType, ResponseContent } from './types';

const errorFromResponse = (status: number, body: any) => {
switch (body?.code) {
case 'invalid_project_api_key':
return 'Api key is not valid';

default:
return `${status}: ${body?.message || 'Error status code from server'}`;
}
};

const fetchFn = createFetchFunction();

type Params = {
Expand Down Expand Up @@ -70,13 +80,15 @@ async function customFetch(
};

return fetchFn(options.apiUrl + input, init)
.catch((e) => {
throw new Error(
e.message || `Failed to fetch data from "${options.apiUrl}"`
);
})
.then(async (r) => {
if (!r.ok) {
const data = await getResObject(r);
const message = `${r.status}: ${
data?.message || 'Error status code from server'
}`;
throw new Error(message);
throw new Error(errorFromResponse(r.status, data));
}
const result = await getResObject(r);
if (typeof result === 'object' && result !== null) {
Expand Down

0 comments on commit f5702b6

Please sign in to comment.