Skip to content

Commit 9fb6300

Browse files
committed
docs: revised the readme for better clarity
1 parent ab22135 commit 9fb6300

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262

6363
```javascript
6464
// pages/api/example.js
65+
6566
const { errorHandler, BadRequestError } = require('nextjs-centralized-error-handler');
6667
6768
const handler = async (req, res) => {
@@ -125,6 +126,7 @@ Next.js 13 middleware can be defined globally and applied across all routes that
125126
126127
```javascript
127128
// middleware.js (placed at the root of the app)
129+
128130
import { NextResponse } from 'next/server';
129131
130132
export function middleware(req) {
@@ -172,6 +174,7 @@ In Next.js, middleware can be used to validate requests globally. However, it ca
172174
173175
```javascript
174176
// middleware.js
177+
175178
import { NextResponse } from 'next/server';
176179
177180
export function middleware(req) {
@@ -186,6 +189,7 @@ export function middleware(req) {
186189
```
187190
```javascript
188191
// pages/api/example.js
192+
189193
const handler = async (req, res) => {
190194
if (!req.body.name) {
191195
throw new Error('Name is required.'); // This will not be caught by middleware
@@ -203,12 +207,13 @@ export default handler;
203207
2. If `req.body.name` is missing, the `Error` is thrown.
204208
3. However, the middleware will not catch this error, resulting in a generic 500 Internal Server Error.
205209
206-
##### Using nextjs-centralized-error-handler
210+
#### Using `nextjs-centralized-error-handler`
207211
208212
In contrast, `nextjs-centralized-error-handler` provides a higher-order function that captures errors thrown in route handlers.
209213
210214
```javascript
211215
// pages/api/example.js
216+
212217
import { errorHandler, BadRequestError } from 'nextjs-centralized-error-handler';
213218
214219
const handler = async (req, res) => {
@@ -235,12 +240,13 @@ Combining both Next.js middleware and `nextjs-centralized-error-handler` provide
235240
- **Global Request Validation and Authentication:** Use Next.js middleware to handle tasks that need to be applied across multiple routes, such as authentication, authorization, and general request validation.
236241
- **Route-Specific Detailed Error Handling:** Use `nextjs-centralized-error-handler` to manage errors that occur within individual route handlers, allowing for customized and structured error responses tailored to each route's specific needs.
237242
238-
##### Example: Using Both Middleware and `nextjs-centralized-error-handler`
243+
### Example: Using Both Middleware and `nextjs-centralized-error-handler`
239244
240245
**Middleware (middleware.js):**
241246
242247
```javascript
243248
// middleware.js (Next.js Middleware for global authentication)
249+
244250
import { NextResponse } from 'next/server';
245251
246252
export function middleware(req) {
@@ -260,6 +266,7 @@ export const config = {
260266
261267
```javascript
262268
// pages/api/example.js (Using nextjs-centralized-error-handler for route-specific errors)
269+
263270
const { errorHandler, BadRequestError } = require('nextjs-centralized-error-handler');
264271
265272
const handler = async (req, res) => {
@@ -300,6 +307,7 @@ In Express, centralized error handling is achieved through middleware functions,
300307
301308
```javascript
302309
// Traditional Express Approach
310+
303311
const express = require('express');
304312
const app = express();
305313
@@ -323,6 +331,7 @@ With `nextjs-centralized-error-handler`, you get middleware-like behavior tailor
323331
324332
```javascript
325333
// Using nextjs-centralized-error-handler
334+
326335
const { errorHandler, BadRequestError } = require('nextjs-centralized-error-handler');
327336
328337
const handler = async (req, res) => {
@@ -389,6 +398,7 @@ Import `errorHandler` and `custom error classes` into your Next.js API route:
389398
390399
```javascript
391400
// pages/api/someEndpoint.js
401+
392402
const { errorHandler, BadRequestError } = require('nextjs-centralized-error-handler');
393403
394404
const handler = async (req, res) => {
@@ -429,6 +439,7 @@ These classes simplify error creation without hardcoding status codes in each ro
429439
430440
```javascript
431441
// Example: Throwing an UnauthorizedError
442+
432443
throw new UnauthorizedError(); // Defaults to "Unauthorized access. Please log in again."
433444
```
434445

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.6",
3+
"version": "1.0.7",
44
"main": "index.js",
55
"scripts": {
66
"test": "jest",

0 commit comments

Comments
 (0)