|
28 | 28 | - [Custom Error Classes](#custom-error-classes) |
29 | 29 | - [Example Usage with Default Messages](#example-usage-with-default-messages) |
30 | 30 | - [Creating Custom Errors](#creating-custom-errors) |
| 31 | + - [Using `nextjs-centralized-error-handler` with App Router](#using-nextjs-centralized-error-handler-with-app-router) |
31 | 32 | - [Customizing Error Handling Behavior](#customizing-error-handling-behavior) |
32 | 33 | - [Error Handler Options](#error-handler-options) |
33 | 34 | - [Customizing Error Responses](#customizing-error-responses) |
@@ -478,6 +479,50 @@ This example defines a custom ConflictError (HTTP 409), which can be thrown in c |
478 | 479 |
|
479 | 480 | --- |
480 | 481 |
|
| 482 | +### Using `nextjs-centralized-error-handler` with App Router |
| 483 | +
|
| 484 | +In addition to supporting traditional API routes, `nextjs-centralized-error-handler` can also be utilized with the App Router introduced in Next.js 13. Here’s how to implement error handling in your App Router using the package. |
| 485 | +
|
| 486 | +#### Example: Using `nextjs-centralized-error-handler` with the App Router |
| 487 | +
|
| 488 | +##### Creating a Route with Error Handling |
| 489 | +You can create a route in your App Router and use the error handler to manage errors effectively. |
| 490 | +
|
| 491 | +```javascript |
| 492 | +// app/api/hello/route.js |
| 493 | +
|
| 494 | +import { errorHandler, BadRequestError } from 'nextjs-centralized-error-handler'; |
| 495 | +
|
| 496 | +const handler = async (req) => { |
| 497 | + const { name } = req.body; |
| 498 | +
|
| 499 | + if (!name) { |
| 500 | + throw new BadRequestError('Name is required.'); // This error will be handled by the errorHandler |
| 501 | + } |
| 502 | +
|
| 503 | + return new Response(JSON.stringify({ message: `Hello, ${name}!` }), { |
| 504 | + status: 200, |
| 505 | + headers: { 'Content-Type': 'application/json' }, |
| 506 | + }); |
| 507 | +}; |
| 508 | +
|
| 509 | +// Wrap the handler with errorHandler for centralized error management |
| 510 | +
|
| 511 | +export const GET = errorHandler(handler); |
| 512 | +export const POST = errorHandler(handler); // Example for handling POST requests |
| 513 | +``` |
| 514 | +
|
| 515 | +#### Explanation |
| 516 | +
|
| 517 | +- **Handler Function**: The handler function processes incoming requests. It checks for a `name` parameter and throws a `BadRequestError` if it's missing. |
| 518 | +- **Error Handling**: The handler is wrapped with `errorHandler`, which captures any errors thrown during execution and returns a structured error response. |
| 519 | +
|
| 520 | +#### Error Handling in App Router |
| 521 | +
|
| 522 | +Using the App Router allows for a clean and structured way to manage errors while leveraging the powerful capabilities of `nextjs-centralized-error-handler`. By combining both, you ensure that your application handles errors effectively, regardless of the routing method used. |
| 523 | +
|
| 524 | +--- |
| 525 | +
|
481 | 526 | ## Customizing Error Handling Behavior |
482 | 527 | Beyond custom errors, this package allows developers to fully control the behavior of error handling by: |
483 | 528 |
|
|
0 commit comments