コンテンツへスキップ

13

エラー処理

前の章では、Server Actionsを使用してデータを変更する方法を学びました。JavaScriptのtry/catchステートメントとNext.js APIを使用して、エラーを適切に処理する方法を見てみましょう。

この章では...

以下に、取り上げるトピックを示します。

特別なerror.tsxファイルを使用してルートセグメントのエラーをキャッチし、ユーザーにフォールバックUIを表示する方法。

notFound関数とnot-foundファイルを使用して、404エラー(存在しないリソースの場合)を処理する方法。

Server Actionsにtry/catchを追加する

まず、JavaScriptのtry/catchステートメントをServer Actionsに追加して、エラーを適切に処理できるようにしましょう。

これを行う方法を知っている場合は、数分かけてServer Actionsを更新するか、以下のコードをコピーできます。

redirecttry/catchブロックの外側で呼び出されていることに注意してください。これは、redirectがエラーをスローすることで動作するため、catchブロックによってキャッチされるためです。これを回避するには、try/catchredirectを呼び出すことができます。redirectは、tryが成功した場合にのみ到達可能です。

次に、Server Actionでエラーがスローされた場合に何が起こるかを確認しましょう。これを行うには、エラーをより早くスローできます。たとえば、deleteInvoiceアクションで、関数の先頭でエラーをスローします。

/app/lib/actions.ts
export async function deleteInvoice(id: string) {
  throw new Error('Failed to Delete Invoice');
 
  // Unreachable code block
  try {
    await sql`DELETE FROM invoices WHERE id = ${id}`;
    revalidatePath('/dashboard/invoices');
    return { message: 'Deleted Invoice' };
  } catch (error) {
    return { message: 'Database Error: Failed to Delete Invoice' };
  }
}

請求書を削除しようとすると、localhostでエラーが表示されるはずです。テスト後、次のセクションに進む前に、このエラーを削除してください。

これらのエラーは、開発中に潜在的な問題を早期にキャッチできるため、役立ちます。ただし、突然の失敗を回避し、アプリケーションの実行を継続できるようにするために、ユーザーにエラーを表示することも必要です。

ここで、Next.jsのerror.tsxファイルが登場します。

error.tsxですべてのエラーを処理する

error.tsxファイルは、ルートセグメントのUI境界を定義するために使用できます。予期しないエラーのキャッチオールとして機能し、フォールバックUIをユーザーに表示することができます。

/dashboard/invoicesフォルダー内に、error.tsxという新しいファイルを作成し、次のコードを貼り付けます。

/dashboard/invoices/error.tsx
'use client';
 
import { useEffect } from 'react';
 
export default function Error({
  error,
  reset,
}: {
  error: Error & { digest?: string };
  reset: () => void;
}) {
  useEffect(() => {
    // Optionally log the error to an error reporting service
    console.error(error);
  }, [error]);
 
  return (
    <main className="flex h-full flex-col items-center justify-center">
      <h2 className="text-center">Something went wrong!</h2>
      <button
        className="mt-4 rounded-md bg-blue-500 px-4 py-2 text-sm text-white transition-colors hover:bg-blue-400"
        onClick={
          // Attempt to recover by trying to re-render the invoices route
          () => reset()
        }
      >
        Try again
      </button>
    </main>
  );
}

上記のコードについて、いくつか気づくことがあるでしょう。

  • "use client" - error.tsxはクライアントコンポーネントである必要があります。
  • 2つのpropsを受け入れます。
    • error:このオブジェクトは、JavaScriptのネイティブErrorオブジェクトのインスタンスです。
    • reset:これはエラー境界をリセットする関数です。実行すると、関数はルートセグメントを再レンダリングしようとします。

もう一度請求書を削除しようとすると、次のUIが表示されるはずです。

The error.tsx file showing the props it accepts

notFound関数を使用して404エラーを処理する

エラーを適切に処理するもう1つの方法は、notFound関数を使用することです。error.tsxすべてのエラーをキャッチするのに役立ちますが、notFoundは存在しないリソースをフェッチしようとした場合に使用できます。

たとえば、http://localhost:3000/dashboard/invoices/2e94d1ed-d220-449f-9f11-f0bbceed9645/editにアクセスしてください。

これは、データベースに存在しない偽のUUIDです。

error.tsxが定義されている/invoicesの子ルートであるため、error.tsxがすぐに起動するのがわかります。

ただし、より具体的にしたい場合は、404エラーを表示して、アクセスしようとしているリソースが見つからなかったことをユーザーに伝えることができます。

data.tsfetchInvoiceById関数に移動し、返されたinvoiceをコンソールログに記録することで、リソースが見つからなかったことを確認できます。

/app/lib/data.ts
export async function fetchInvoiceById(id: string) {
  noStore();
  try {
    // ...
 
    console.log(invoice); // Invoice is an empty array []
    return invoice[0];
  } catch (error) {
    console.error('Database Error:', error);
    throw new Error('Failed to fetch invoice.');
  }
}

請求書がデータベースに存在しないことがわかったので、notFoundを使用して処理しましょう。/dashboard/invoices/[id]/edit/page.tsxに移動し、'next/navigation'から{ notFound }をインポートします。

次に、条件付きを使用して、請求書が存在しない場合にnotFoundを呼び出すことができます。

/dashboard/invoices/[id]/edit/page.tsx
import { fetchInvoiceById, fetchCustomers } from '@/app/lib/data';
import { updateInvoice } from '@/app/lib/actions';
import { notFound } from 'next/navigation';
 
export default async function Page(props: { params: Promise<{ id: string }> }) {
  const params = await props.params;
  const id = params.id;
  const [invoice, customers] = await Promise.all([
    fetchInvoiceById(id),
    fetchCustomers(),
  ]);
 
  if (!invoice) {
    notFound();
  }
 
  // ...
}

完璧です!特定の請求書が見つからない場合、<Page>はエラーをスローするようになります。エラーUIをユーザーに表示するには、/editフォルダー内にnot-found.tsxファイルを作成します。

The not-found.tsx file inside the edit folder

次に、not-found.tsxファイル内に、次のコードを貼り付けます。

/dashboard/invoices/[id]/edit/not-found.tsx
import Link from 'next/link';
import { FaceFrownIcon } from '@heroicons/react/24/outline';
 
export default function NotFound() {
  return (
    <main className="flex h-full flex-col items-center justify-center gap-2">
      <FaceFrownIcon className="w-10 text-gray-400" />
      <h2 className="text-xl font-semibold">404 Not Found</h2>
      <p>Could not find the requested invoice.</p>
      <Link
        href="/dashboard/invoices"
        className="mt-4 rounded-md bg-blue-500 px-4 py-2 text-sm text-white transition-colors hover:bg-blue-400"
      >
        Go Back
      </Link>
    </main>
  );
}

ルートをリフレッシュすると、次のUIが表示されるはずです。

404 Not Found Page

覚えておいていただきたいのは、notFounderror.tsxよりも優先されるため、より具体的なエラーを処理したい場合は利用できるということです!

さらに詳しく読む

Next.jsのエラー処理について詳しくは、次のドキュメントをご覧ください。

チャプターを完了しました13

素晴らしい。これでアプリケーションでエラーを適切に処理できるようになりました。

次へ

14: アクセシビリティの向上

ユーザーエクスペリエンスを向上させる方法を探求し続けましょう。サーバーサイドのフォームバリデーションとアクセシビリティの向上について学びます。