error-shield 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +201 -0
- package/dist/index.d.ts +83 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +123 -0
- package/dist/index.js.map +1 -0
- package/package.json +39 -0
package/README.md
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
# error-handler
|
|
2
|
+
|
|
3
|
+
A comprehensive error handling utility for Node.js applications. Provides structured error handling, formatting, and Express.js middleware support.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🎯 **Custom Error Class** - Extendable `AppError` class with status codes and context
|
|
8
|
+
- 📦 **Error Formatting** - Consistent error formatting with customizable options
|
|
9
|
+
- 🔄 **Async Handler** - Wrapper for async functions to catch errors
|
|
10
|
+
- 🚀 **Express Middleware** - Ready-to-use Express.js error handler middleware
|
|
11
|
+
- 🛠️ **Error Creators** - Pre-built error creators for common HTTP status codes
|
|
12
|
+
- 📝 **TypeScript Support** - Full TypeScript definitions included
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install error-handler
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
### Basic Usage
|
|
23
|
+
|
|
24
|
+
```javascript
|
|
25
|
+
const { AppError, handleError, ErrorCreators } = require('error-handler');
|
|
26
|
+
|
|
27
|
+
// Create a custom error
|
|
28
|
+
const error = new AppError('Something went wrong', 500, 'CUSTOM_ERROR', {
|
|
29
|
+
userId: 123,
|
|
30
|
+
action: 'updateProfile'
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// Handle and format error
|
|
34
|
+
const errorDetails = handleError(error, {
|
|
35
|
+
includeStack: true,
|
|
36
|
+
includeTimestamp: true
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
console.log(errorDetails);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Using Error Creators
|
|
43
|
+
|
|
44
|
+
```javascript
|
|
45
|
+
const { ErrorCreators } = require('error-handler');
|
|
46
|
+
|
|
47
|
+
// Bad Request (400)
|
|
48
|
+
throw ErrorCreators.badRequest('Invalid input provided', { field: 'email' });
|
|
49
|
+
|
|
50
|
+
// Unauthorized (401)
|
|
51
|
+
throw ErrorCreators.unauthorized('Authentication required');
|
|
52
|
+
|
|
53
|
+
// Not Found (404)
|
|
54
|
+
throw ErrorCreators.notFound('User not found', { userId: 123 });
|
|
55
|
+
|
|
56
|
+
// Validation Error (422)
|
|
57
|
+
throw ErrorCreators.validationError('Email is required', { field: 'email' });
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Express.js Middleware
|
|
61
|
+
|
|
62
|
+
```javascript
|
|
63
|
+
const express = require('express');
|
|
64
|
+
const { expressErrorHandler, ErrorCreators } = require('error-handler');
|
|
65
|
+
|
|
66
|
+
const app = express();
|
|
67
|
+
|
|
68
|
+
// Your routes
|
|
69
|
+
app.get('/users/:id', async (req, res, next) => {
|
|
70
|
+
const user = await getUserById(req.params.id);
|
|
71
|
+
if (!user) {
|
|
72
|
+
throw ErrorCreators.notFound('User not found', { userId: req.params.id });
|
|
73
|
+
}
|
|
74
|
+
res.json(user);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// Error handler middleware (must be last)
|
|
78
|
+
app.use(expressErrorHandler({
|
|
79
|
+
includeStack: process.env.NODE_ENV !== 'production',
|
|
80
|
+
includeTimestamp: true
|
|
81
|
+
}));
|
|
82
|
+
|
|
83
|
+
app.listen(3000);
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Async Handler Wrapper
|
|
87
|
+
|
|
88
|
+
```javascript
|
|
89
|
+
const { asyncHandler } = require('error-handler');
|
|
90
|
+
|
|
91
|
+
// Wrap async route handlers
|
|
92
|
+
app.get('/api/data', asyncHandler(async (req, res) => {
|
|
93
|
+
const data = await fetchData();
|
|
94
|
+
res.json(data);
|
|
95
|
+
}));
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Custom Logger
|
|
99
|
+
|
|
100
|
+
```javascript
|
|
101
|
+
const { handleError } = require('error-handler');
|
|
102
|
+
|
|
103
|
+
const errorDetails = handleError(error, {
|
|
104
|
+
logger: (errorDetails) => {
|
|
105
|
+
// Custom logging logic
|
|
106
|
+
console.error('Error occurred:', errorDetails);
|
|
107
|
+
// Send to logging service, etc.
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## API Reference
|
|
113
|
+
|
|
114
|
+
### `AppError`
|
|
115
|
+
|
|
116
|
+
Custom error class that extends the native `Error` class.
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
class AppError extends Error {
|
|
120
|
+
code?: string;
|
|
121
|
+
statusCode?: number;
|
|
122
|
+
context?: Record<string, any>;
|
|
123
|
+
isOperational: boolean;
|
|
124
|
+
}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
**Constructor:**
|
|
128
|
+
- `message: string` - Error message
|
|
129
|
+
- `statusCode: number` - HTTP status code (default: 500)
|
|
130
|
+
- `code?: string` - Error code
|
|
131
|
+
- `context?: Record<string, any>` - Additional context
|
|
132
|
+
|
|
133
|
+
### `formatError(error, options?)`
|
|
134
|
+
|
|
135
|
+
Formats an error into a structured object.
|
|
136
|
+
|
|
137
|
+
**Parameters:**
|
|
138
|
+
- `error: Error | AppError` - The error to format
|
|
139
|
+
- `options: ErrorHandlerOptions` - Formatting options
|
|
140
|
+
|
|
141
|
+
**Returns:** `ErrorDetails`
|
|
142
|
+
|
|
143
|
+
### `handleError(error, options?)`
|
|
144
|
+
|
|
145
|
+
Handles errors with optional logging and formatting.
|
|
146
|
+
|
|
147
|
+
**Parameters:**
|
|
148
|
+
- `error: Error | AppError` - The error to handle
|
|
149
|
+
- `options: ErrorHandlerOptions` - Handler options
|
|
150
|
+
|
|
151
|
+
**Returns:** `ErrorDetails`
|
|
152
|
+
|
|
153
|
+
### `asyncHandler(fn)`
|
|
154
|
+
|
|
155
|
+
Wraps an async function to catch errors.
|
|
156
|
+
|
|
157
|
+
**Parameters:**
|
|
158
|
+
- `fn: Function` - Async function to wrap
|
|
159
|
+
|
|
160
|
+
**Returns:** Wrapped function
|
|
161
|
+
|
|
162
|
+
### `expressErrorHandler(options?)`
|
|
163
|
+
|
|
164
|
+
Creates an Express.js error handler middleware.
|
|
165
|
+
|
|
166
|
+
**Parameters:**
|
|
167
|
+
- `options: ErrorHandlerOptions` - Handler options
|
|
168
|
+
|
|
169
|
+
**Returns:** Express middleware function
|
|
170
|
+
|
|
171
|
+
### `ErrorCreators`
|
|
172
|
+
|
|
173
|
+
Pre-built error creators for common HTTP status codes:
|
|
174
|
+
|
|
175
|
+
- `badRequest(message, context?)` - 400
|
|
176
|
+
- `unauthorized(message?, context?)` - 401
|
|
177
|
+
- `forbidden(message?, context?)` - 403
|
|
178
|
+
- `notFound(message?, context?)` - 404
|
|
179
|
+
- `conflict(message, context?)` - 409
|
|
180
|
+
- `validationError(message, context?)` - 422
|
|
181
|
+
- `internalServerError(message?, context?)` - 500
|
|
182
|
+
- `serviceUnavailable(message?, context?)` - 503
|
|
183
|
+
|
|
184
|
+
## TypeScript Support
|
|
185
|
+
|
|
186
|
+
This package includes full TypeScript definitions:
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
import { AppError, ErrorCreators, handleError } from 'error-handler';
|
|
190
|
+
|
|
191
|
+
const error: AppError = ErrorCreators.notFound('User not found');
|
|
192
|
+
const details = handleError(error);
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
## License
|
|
196
|
+
|
|
197
|
+
ISC
|
|
198
|
+
|
|
199
|
+
## Author
|
|
200
|
+
|
|
201
|
+
Gopinath Kathirvel
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error Handler - A comprehensive error handling utility for Node.js
|
|
3
|
+
*
|
|
4
|
+
* Provides utilities for error handling, formatting, and logging
|
|
5
|
+
*/
|
|
6
|
+
export interface ErrorDetails {
|
|
7
|
+
message: string;
|
|
8
|
+
code?: string;
|
|
9
|
+
statusCode?: number;
|
|
10
|
+
stack?: string;
|
|
11
|
+
timestamp?: string;
|
|
12
|
+
context?: Record<string, any>;
|
|
13
|
+
}
|
|
14
|
+
export interface ErrorHandlerOptions {
|
|
15
|
+
includeStack?: boolean;
|
|
16
|
+
includeTimestamp?: boolean;
|
|
17
|
+
format?: 'json' | 'string';
|
|
18
|
+
logger?: (error: ErrorDetails) => void;
|
|
19
|
+
context?: Record<string, any>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Custom Error class with additional properties
|
|
23
|
+
*/
|
|
24
|
+
export declare class AppError extends Error {
|
|
25
|
+
code?: string;
|
|
26
|
+
statusCode?: number;
|
|
27
|
+
context?: Record<string, any>;
|
|
28
|
+
isOperational: boolean;
|
|
29
|
+
constructor(message: string, statusCode?: number, code?: string, context?: Record<string, any>);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Formats an error into a structured object
|
|
33
|
+
*/
|
|
34
|
+
export declare function formatError(error: Error | AppError, options?: ErrorHandlerOptions): ErrorDetails;
|
|
35
|
+
/**
|
|
36
|
+
* Handles errors with optional logging and formatting
|
|
37
|
+
*/
|
|
38
|
+
export declare function handleError(error: Error | AppError, options?: ErrorHandlerOptions): ErrorDetails;
|
|
39
|
+
/**
|
|
40
|
+
* Async error wrapper - catches errors from async functions
|
|
41
|
+
*/
|
|
42
|
+
export declare function asyncHandler<T extends (...args: any[]) => Promise<any>>(fn: T): T;
|
|
43
|
+
/**
|
|
44
|
+
* Express.js error handler middleware
|
|
45
|
+
*/
|
|
46
|
+
export declare function expressErrorHandler(options?: ErrorHandlerOptions): (err: Error | AppError, req: any, res: any, next: any) => void;
|
|
47
|
+
/**
|
|
48
|
+
* Creates a standardized error response
|
|
49
|
+
*/
|
|
50
|
+
export declare function createErrorResponse(message: string, statusCode?: number, code?: string, context?: Record<string, any>): AppError;
|
|
51
|
+
/**
|
|
52
|
+
* Common error creators
|
|
53
|
+
*/
|
|
54
|
+
export declare const ErrorCreators: {
|
|
55
|
+
badRequest: (message: string, context?: Record<string, any>) => AppError;
|
|
56
|
+
unauthorized: (message?: string, context?: Record<string, any>) => AppError;
|
|
57
|
+
forbidden: (message?: string, context?: Record<string, any>) => AppError;
|
|
58
|
+
notFound: (message?: string, context?: Record<string, any>) => AppError;
|
|
59
|
+
conflict: (message: string, context?: Record<string, any>) => AppError;
|
|
60
|
+
validationError: (message: string, context?: Record<string, any>) => AppError;
|
|
61
|
+
internalServerError: (message?: string, context?: Record<string, any>) => AppError;
|
|
62
|
+
serviceUnavailable: (message?: string, context?: Record<string, any>) => AppError;
|
|
63
|
+
};
|
|
64
|
+
declare const _default: {
|
|
65
|
+
AppError: typeof AppError;
|
|
66
|
+
formatError: typeof formatError;
|
|
67
|
+
handleError: typeof handleError;
|
|
68
|
+
asyncHandler: typeof asyncHandler;
|
|
69
|
+
expressErrorHandler: typeof expressErrorHandler;
|
|
70
|
+
createErrorResponse: typeof createErrorResponse;
|
|
71
|
+
ErrorCreators: {
|
|
72
|
+
badRequest: (message: string, context?: Record<string, any>) => AppError;
|
|
73
|
+
unauthorized: (message?: string, context?: Record<string, any>) => AppError;
|
|
74
|
+
forbidden: (message?: string, context?: Record<string, any>) => AppError;
|
|
75
|
+
notFound: (message?: string, context?: Record<string, any>) => AppError;
|
|
76
|
+
conflict: (message: string, context?: Record<string, any>) => AppError;
|
|
77
|
+
validationError: (message: string, context?: Record<string, any>) => AppError;
|
|
78
|
+
internalServerError: (message?: string, context?: Record<string, any>) => AppError;
|
|
79
|
+
serviceUnavailable: (message?: string, context?: Record<string, any>) => AppError;
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
export default _default;
|
|
83
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1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M,EAAE,GAAG,CAAC;4BAGnD,MAAM,YAAY,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;mCAG9B,MAAM,YAAY,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;wCAGjC,MAAM,YAAsC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;uCAGhE,MAAM,YAAoC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;;;AAK7F,wBAQE"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Error Handler - A comprehensive error handling utility for Node.js
|
|
4
|
+
*
|
|
5
|
+
* Provides utilities for error handling, formatting, and logging
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.ErrorCreators = exports.AppError = void 0;
|
|
9
|
+
exports.formatError = formatError;
|
|
10
|
+
exports.handleError = handleError;
|
|
11
|
+
exports.asyncHandler = asyncHandler;
|
|
12
|
+
exports.expressErrorHandler = expressErrorHandler;
|
|
13
|
+
exports.createErrorResponse = createErrorResponse;
|
|
14
|
+
/**
|
|
15
|
+
* Custom Error class with additional properties
|
|
16
|
+
*/
|
|
17
|
+
class AppError extends Error {
|
|
18
|
+
constructor(message, statusCode = 500, code, context) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.name = this.constructor.name;
|
|
21
|
+
this.statusCode = statusCode;
|
|
22
|
+
this.code = code;
|
|
23
|
+
this.context = context;
|
|
24
|
+
this.isOperational = true;
|
|
25
|
+
if (Error.captureStackTrace) {
|
|
26
|
+
Error.captureStackTrace(this, this.constructor);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.AppError = AppError;
|
|
31
|
+
/**
|
|
32
|
+
* Formats an error into a structured object
|
|
33
|
+
*/
|
|
34
|
+
function formatError(error, options = {}) {
|
|
35
|
+
const { includeStack = false, includeTimestamp = true, context: optionsContext = {}, } = options;
|
|
36
|
+
const mergedContext = error instanceof AppError
|
|
37
|
+
? { ...error.context, ...optionsContext }
|
|
38
|
+
: optionsContext;
|
|
39
|
+
const errorDetails = {
|
|
40
|
+
message: error.message,
|
|
41
|
+
...(includeTimestamp && { timestamp: new Date().toISOString() }),
|
|
42
|
+
...(includeStack && error.stack && { stack: error.stack }),
|
|
43
|
+
...(error instanceof AppError && {
|
|
44
|
+
code: error.code,
|
|
45
|
+
statusCode: error.statusCode,
|
|
46
|
+
}),
|
|
47
|
+
...(Object.keys(mergedContext).length > 0 && { context: mergedContext }),
|
|
48
|
+
};
|
|
49
|
+
return errorDetails;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Handles errors with optional logging and formatting
|
|
53
|
+
*/
|
|
54
|
+
function handleError(error, options = {}) {
|
|
55
|
+
const errorDetails = formatError(error, options);
|
|
56
|
+
if (options.logger) {
|
|
57
|
+
options.logger(errorDetails);
|
|
58
|
+
}
|
|
59
|
+
return errorDetails;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Async error wrapper - catches errors from async functions
|
|
63
|
+
*/
|
|
64
|
+
function asyncHandler(fn) {
|
|
65
|
+
return ((...args) => {
|
|
66
|
+
return Promise.resolve(fn(...args)).catch((error) => {
|
|
67
|
+
throw error;
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Express.js error handler middleware
|
|
73
|
+
*/
|
|
74
|
+
function expressErrorHandler(options = {}) {
|
|
75
|
+
return (err, req, res, next) => {
|
|
76
|
+
const errorDetails = handleError(err, {
|
|
77
|
+
...options,
|
|
78
|
+
context: {
|
|
79
|
+
...options.context,
|
|
80
|
+
method: req.method,
|
|
81
|
+
path: req.path,
|
|
82
|
+
ip: req.ip,
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
const statusCode = err instanceof AppError ? err.statusCode || 500 : 500;
|
|
86
|
+
if (options.format === 'string') {
|
|
87
|
+
res.status(statusCode).send(errorDetails.message);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
res.status(statusCode).json(errorDetails);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Creates a standardized error response
|
|
96
|
+
*/
|
|
97
|
+
function createErrorResponse(message, statusCode = 500, code, context) {
|
|
98
|
+
return new AppError(message, statusCode, code, context);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Common error creators
|
|
102
|
+
*/
|
|
103
|
+
exports.ErrorCreators = {
|
|
104
|
+
badRequest: (message, context) => new AppError(message, 400, 'BAD_REQUEST', context),
|
|
105
|
+
unauthorized: (message = 'Unauthorized', context) => new AppError(message, 401, 'UNAUTHORIZED', context),
|
|
106
|
+
forbidden: (message = 'Forbidden', context) => new AppError(message, 403, 'FORBIDDEN', context),
|
|
107
|
+
notFound: (message = 'Not Found', context) => new AppError(message, 404, 'NOT_FOUND', context),
|
|
108
|
+
conflict: (message, context) => new AppError(message, 409, 'CONFLICT', context),
|
|
109
|
+
validationError: (message, context) => new AppError(message, 422, 'VALIDATION_ERROR', context),
|
|
110
|
+
internalServerError: (message = 'Internal Server Error', context) => new AppError(message, 500, 'INTERNAL_SERVER_ERROR', context),
|
|
111
|
+
serviceUnavailable: (message = 'Service Unavailable', context) => new AppError(message, 503, 'SERVICE_UNAVAILABLE', context),
|
|
112
|
+
};
|
|
113
|
+
// Default export
|
|
114
|
+
exports.default = {
|
|
115
|
+
AppError,
|
|
116
|
+
formatError,
|
|
117
|
+
handleError,
|
|
118
|
+
asyncHandler,
|
|
119
|
+
expressErrorHandler,
|
|
120
|
+
createErrorResponse,
|
|
121
|
+
ErrorCreators: exports.ErrorCreators,
|
|
122
|
+
};
|
|
123
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;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AE,GAAG,EAAE,UAAU,EAAE,OAAO,CAAC;IAEjD,eAAe,EAAE,CAAC,OAAe,EAAE,OAA6B,EAAE,EAAE,CAClE,IAAI,QAAQ,CAAC,OAAO,EAAE,GAAG,EAAE,kBAAkB,EAAE,OAAO,CAAC;IAEzD,mBAAmB,EAAE,CAAC,UAAkB,uBAAuB,EAAE,OAA6B,EAAE,EAAE,CAChG,IAAI,QAAQ,CAAC,OAAO,EAAE,GAAG,EAAE,uBAAuB,EAAE,OAAO,CAAC;IAE9D,kBAAkB,EAAE,CAAC,UAAkB,qBAAqB,EAAE,OAA6B,EAAE,EAAE,CAC7F,IAAI,QAAQ,CAAC,OAAO,EAAE,GAAG,EAAE,qBAAqB,EAAE,OAAO,CAAC;CAC7D,CAAC;AAEF,iBAAiB;AACjB,kBAAe;IACb,QAAQ;IACR,WAAW;IACX,WAAW;IACX,YAAY;IACZ,mBAAmB;IACnB,mBAAmB;IACnB,aAAa,EAAb,qBAAa;CACd,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "error-shield",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A comprehensive error handling utility for Node.js applications with Express.js middleware support",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"README.md"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"prepublishOnly": "npm run build",
|
|
14
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"error",
|
|
18
|
+
"error-handler",
|
|
19
|
+
"error-handling",
|
|
20
|
+
"express",
|
|
21
|
+
"middleware",
|
|
22
|
+
"typescript",
|
|
23
|
+
"nodejs",
|
|
24
|
+
"exception",
|
|
25
|
+
"error-management"
|
|
26
|
+
],
|
|
27
|
+
"author": "Gopinath Kathirvel",
|
|
28
|
+
"license": "ISC",
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=14.0.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"typescript": "^5.0.0"
|
|
34
|
+
},
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": ""
|
|
38
|
+
}
|
|
39
|
+
}
|