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

Better error handling. #1150

Merged
merged 3 commits into from
Aug 5, 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
15 changes: 12 additions & 3 deletions app/error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@

export default function Error({ reset }: { reset: () => void }) {
return (
<div>
<h2>Something went wrong.</h2>
<button onClick={() => reset()}>Try again</button>
<div className="mx-auto my-4 flex max-w-xl flex-col rounded-lg border border-neutral-200 bg-white p-8 dark:border-neutral-800 dark:bg-black md:p-12">
<h2 className="text-xl font-bold">Oh no!</h2>
<p className="my-2">
There was an issue with our storefront. This could be a temporary issue, please try your
action again.
</p>
<button
className="mx-auto mt-4 flex w-full items-center justify-center rounded-full bg-blue-600 p-4 tracking-wide text-white hover:opacity-90"
onClick={() => reset()}
>
Try Again
</button>
</div>
);
}
6 changes: 5 additions & 1 deletion app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import { ReactNode, Suspense } from 'react';
import './globals.css';

const { TWITTER_CREATOR, TWITTER_SITE, SITE_NAME } = process.env;
const baseUrl = process.env.NEXT_PUBLIC_VERCEL_URL
? `https://${process.env.NEXT_PUBLIC_VERCEL_URL}`
: 'http://localhost:3000';

export const metadata = {
metadataBase: new URL(baseUrl),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was also throwing a console warning in local dev.

title: {
default: SITE_NAME,
default: SITE_NAME!,
template: `%s | ${SITE_NAME}`
},
robots: {
Expand Down
19 changes: 10 additions & 9 deletions components/cart/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { addToCart, createCart, getCart, removeFromCart, updateCart } from 'lib/shopify';
import { cookies } from 'next/headers';

export const addItem = async (variantId: string | undefined): Promise<Error | undefined> => {
export const addItem = async (variantId: string | undefined): Promise<String | undefined> => {
let cartId = cookies().get('cartId')?.value;
let cart;

Expand All @@ -18,25 +18,26 @@ export const addItem = async (variantId: string | undefined): Promise<Error | un
}

if (!variantId) {
return new Error('Missing variantId');
return 'Missing product variant ID';
}

try {
await addToCart(cartId, [{ merchandiseId: variantId, quantity: 1 }]);
} catch (e) {
return new Error('Error adding item', { cause: e });
return 'Error adding item to cart';
}
};

export const removeItem = async (lineId: string): Promise<Error | undefined> => {
export const removeItem = async (lineId: string): Promise<String | undefined> => {
const cartId = cookies().get('cartId')?.value;

if (!cartId) {
return new Error('Missing cartId');
return 'Missing cart ID';
}
try {
await removeFromCart(cartId, [lineId]);
} catch (e) {
return new Error('Error removing item', { cause: e });
return 'Error removing item from cart';
}
};

Expand All @@ -48,11 +49,11 @@ export const updateItemQuantity = async ({
lineId: string;
variantId: string;
quantity: number;
}): Promise<Error | undefined> => {
}): Promise<String | undefined> => {
const cartId = cookies().get('cartId')?.value;

if (!cartId) {
return new Error('Missing cartId');
return 'Missing cart ID';
}
try {
await updateCart(cartId, [
Expand All @@ -63,6 +64,6 @@ export const updateItemQuantity = async ({
}
]);
} catch (e) {
return new Error('Error updating item quantity', { cause: e });
return 'Error updating item quantity';
}
};
4 changes: 2 additions & 2 deletions components/cart/add-to-cart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export function AddToCart({
const error = await addItem(selectedVariantId);

if (error) {
alert(error);
return;
// Trigger the error boundary in the root error.js
throw new Error(error.toString());
}

router.refresh();
Expand Down
4 changes: 2 additions & 2 deletions components/cart/delete-item-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export default function DeleteItemButton({ item }: { item: CartItem }) {
const error = await removeItem(item.id);

if (error) {
alert(error);
return;
// Trigger the error boundary in the root error.js
throw new Error(error.toString());
}

router.refresh();
Expand Down
4 changes: 2 additions & 2 deletions components/cart/edit-item-quantity-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export default function EditItemQuantityButton({
});

if (error) {
alert(error);
return;
// Trigger the error boundary in the root error.js
throw new Error(error.toString());
}

router.refresh();
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"@headlessui/react": "^1.7.15",
"@heroicons/react": "^2.0.18",
"clsx": "^2.0.0",
"next": "13.4.12",
"next": "13.4.13-canary.15",
"react": "18.2.0",
"react-dom": "18.2.0"
},
Expand All @@ -43,7 +43,7 @@
"eslint-plugin-unicorn": "^48.0.0",
"lint-staged": "^13.2.3",
"postcss": "^8.4.27",
"prettier": "^3.0.0",
"prettier": "3.0.1",
"prettier-plugin-tailwindcss": "^0.4.1",
"tailwindcss": "^3.3.3",
"typescript": "5.1.6"
Expand Down
85 changes: 41 additions & 44 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.