Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/error-handling #4

Merged
merged 2 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions app/error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,22 @@ import { Balancer } from 'react-wrap-balancer';

import { Icons } from '~/components/icons';
import { Button } from '~/components/ui/button';
import { getErrorDetail } from '~/lib/exceptions';

interface RootError {
error: Error & { digest?: string };
reset: () => void;
}

export default function Error({ error, reset }: RootError) {
const errorDetail = getErrorDetail(error.message);

export default function Error({ reset }: RootError) {
return (
<main className="container flex flex-1 items-center py-4">
<div className="flex w-full flex-col items-center rounded-lg border p-6 text-center">
<div className="relative flex w-full flex-col items-center rounded-lg border p-6 text-center">
<Icons.ServerError size={50} className="mb-2" />
<h1 className="mb-1 text-xl font-bold">
<Balancer>{errorDetail.title}</Balancer>
<Balancer>Something went wrong!</Balancer>
</h1>
<p className="mb-4 text-sm text-muted-foreground">
<Balancer>{errorDetail.description}</Balancer>
<Balancer>Please try again later</Balancer>
</p>

<Button className="w-full gap-2" onClick={reset}>
Expand Down
27 changes: 0 additions & 27 deletions lib/exceptions.ts

This file was deleted.

37 changes: 6 additions & 31 deletions lib/spoonacular.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { QuotaExceededError, UnexpectedError } from '~/lib/exceptions';
import { RecipeInformation } from '~/types/spoonacular/recipe-information';
import { RecipeSearchResult } from '~/types/spoonacular/recipe-search';

Expand Down Expand Up @@ -30,12 +29,8 @@ class SpoonacularAPI {
next: { revalidate: 60 * 60 * 24 },
});

if (response.status === 402) {
throw new QuotaExceededError();
}

if (!response.ok) {
throw new UnexpectedError();
throw new Error('Failed to fetch random daily recipes');
}

const data = await response.json();
Expand All @@ -56,12 +51,8 @@ class SpoonacularAPI {
next: { revalidate: 60 * 60 * 24 * 7 },
});

if (response.status === 402) {
throw new QuotaExceededError();
}

if (!response.ok) {
throw new UnexpectedError();
throw new Error('Failed to fetch random vegetarian recipes');
}

const data = await response.json();
Expand All @@ -80,12 +71,8 @@ class SpoonacularAPI {
next: { revalidate: 60 * 60 * 24 * 7 },
});

if (response.status === 402) {
throw new QuotaExceededError();
}

if (!response.ok) {
throw new UnexpectedError();
throw new Error('Failed to fetch random dessert recipes');
}

const data = await response.json();
Expand All @@ -101,12 +88,8 @@ class SpoonacularAPI {
next: { revalidate: 60 * 60 * 24 },
});

if (response.status === 402) {
throw new QuotaExceededError();
}

if (!response.ok) {
throw new UnexpectedError();
throw new Error('Failed to fetch random food trivia');
}

const data = await response.json();
Expand All @@ -124,13 +107,9 @@ class SpoonacularAPI {
cache: 'force-cache',
});

if (response.status === 402) {
throw new QuotaExceededError();
}

if (!response.ok) {
if (response.status === 404) return null;
throw new UnexpectedError();
throw new Error(`Failed to fetch recipe information with id "${id}"`);
}

const data = await response.json();
Expand All @@ -148,12 +127,8 @@ class SpoonacularAPI {
cache: 'force-cache',
});

if (response.status === 402) {
throw new QuotaExceededError();
}

if (!response.ok) {
throw new UnexpectedError();
throw new Error('Failed to fetch recipes by query');
}

const data = await response.json();
Expand Down