Skip to content

Commit fa83def

Browse files
committed
docs: update README with usage instructions for app router
1 parent 5cb9603 commit fa83def

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
- [Custom Error Classes](#custom-error-classes)
2929
- [Example Usage with Default Messages](#example-usage-with-default-messages)
3030
- [Creating Custom Errors](#creating-custom-errors)
31+
- [Using `nextjs-centralized-error-handler` with App Router](#using-nextjs-centralized-error-handler-with-app-router)
3132
- [Customizing Error Handling Behavior](#customizing-error-handling-behavior)
3233
- [Error Handler Options](#error-handler-options)
3334
- [Customizing Error Responses](#customizing-error-responses)
@@ -478,6 +479,50 @@ This example defines a custom ConflictError (HTTP 409), which can be thrown in c
478479
479480
---
480481
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+
481526
## Customizing Error Handling Behavior
482527
Beyond custom errors, this package allows developers to fully control the behavior of error handling by:
483528

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nextjs-centralized-error-handler",
3-
"version": "1.0.10",
3+
"version": "1.0.11",
44
"main": "src/index.js",
55
"scripts": {
66
"test": "jest",

0 commit comments

Comments
 (0)