You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A comprehensiveerror-handling package designed specifically for Next.js applications. This package provides centralized error handling through custom error classes and higher-order functions, solving common limitations in Next.js API routes. By serializing error messages in a frontend-compatible format, it enhances frontend-backend integration, making it easy to handle errors across the entire stack.
9
+
**A comprehensive, secure error-handling package designed specifically for Next.js applications.** By leveraging centralized error handling through custom error classes and a higher-order function, this package overcomes limitations in Next.js API routes, where traditional middleware isn’t supported. It catches all errors—both expected and unexpected—ensuring consistent responses, preventing information leakage, and eliminating the need for repetitive `try-catch` blocks. Errors are serialized in a frontend-compatible JSON format, enhancing frontend-backend integration and delivering user-friendly feedback. Ideal for scalable applications, `nextjs-centralized-error-handler` simplifies error management across the entire stack while maintaining robust security.
10
10
11
11
## Table of Contents
12
12
@@ -27,6 +27,7 @@ A comprehensive error-handling package designed specifically for Next.js applica
-[Integration with Logging Services](#integration-with-logging-services)
32
33
-[Enhanced Logging with Sentry](#enhanced-logging-with-sentry)
@@ -263,6 +264,49 @@ Developers can pass a `formatError` function to customize how errors are returne
263
264
264
265
---
265
266
267
+
## Security Considerations
268
+
269
+
### Comprehensive Exception Handling
270
+
The provided `errorHandler` higher-order function catches all exceptions in the route handler—not just those thrown using the package's custom error classes. This approach intercepts any uncaught errors, regardless of their origin, which eliminates the need for repetitive `try-catch` blocks in each route. By wrapping your route handlers with `errorHandler`, you rely on a consistent, centralized error-handling strategy across all routes, minimizing code redundancy and enhancing maintainability.
271
+
272
+
### Preventing Information Leakage
273
+
The `errorHandler` is designed to prevent sensitive information from being exposed to clients:
274
+
275
+
-**Custom Errors Only**: Only errors that are instances of `CustomError` (or its subclasses) will have their `statusCode` and `message` sent to the client. This provides meaningful, user-friendly error messages for known issues.
276
+
277
+
-**Generic Handling of Other Errors**: For unexpected errors (such as those thrown by third-party libraries or unanticipated issues), `errorHandler` defaults to a `statusCode` of 500 and a generic error message, ensuring internal server details are kept private.
278
+
279
+
### Example: Handling Unexpected Errors Safely
280
+
Consider an API route that relies on a third-party library, which may throw errors we can’t predict:
awaitthirdPartyLibrary.doSomething(); // Throws an unexpected error
288
+
};
289
+
290
+
exportdefaulterrorHandler(handler);
291
+
```
292
+
### What Happens Under the Hood
293
+
If `thirdPartyLibrary.doSomething()` throws an error that isn’t a `CustomError`, `errorHandler` will:
294
+
295
+
1.**Set `statusCode`** to 500 (or the configured `defaultStatusCode`).
296
+
2.**Set `message`** to "An internal server error occurred. Please try again later." (or `defaultMessage` if configured).
297
+
3.**Prevent Information Leakage**: Ensures no sensitive details (e.g., the original error message or stack trace) are sent to the client.
298
+
4.**Log the Error Server-Side**: Uses the provided logger for internal monitoring to record the error.
299
+
300
+
### Note on Error Handling Strategy
301
+
The `errorHandler` function distinguishes between custom errors and unexpected errors:
302
+
303
+
-**Custom Errors (`CustomError` instances)**: The specific `statusCode` and `message` you define are sent to the client, offering clear and user-friendly feedback for known issues.
304
+
-**Other Errors**: A default `statusCode` and message are used to safeguard against information leakage from unexpected errors.
305
+
306
+
By catching all errors in this way, `nextjs-centralized-error-handler` provides a robust, secure, and unified error-handling solution tailored for Next.js applications, with built-in protections to prevent unintended data exposure.
307
+
308
+
---
309
+
266
310
## Examples
267
311
268
312
Here are a few real-world scenarios that showcase the package’s usage:
0 commit comments