@naman_deep_singh/errors-utils 1.0.0 → 1.0.1

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.
Files changed (75) hide show
  1. package/README.md +125 -0
  2. package/dist/{index.d.ts → cjs/index.d.ts} +1 -2
  3. package/dist/{index.js → cjs/index.js} +1 -2
  4. package/dist/{middleware → cjs/middleware/express}/errorConverter.js +1 -1
  5. package/dist/cjs/middleware/express/errorHandler.d.ts +2 -0
  6. package/dist/{middleware → cjs/middleware/express}/errorHandler.js +4 -4
  7. package/dist/cjs/middleware/express/index.d.ts +2 -0
  8. package/dist/cjs/middleware/express/index.js +18 -0
  9. package/dist/esm/error/AppError.d.ts +6 -0
  10. package/{src/error/AppError.ts → dist/esm/error/AppError.js} +1 -8
  11. package/dist/esm/error/BadRequestError.d.ts +4 -0
  12. package/{src/error/BadRequestError.ts → dist/esm/error/BadRequestError.js} +1 -2
  13. package/dist/esm/error/ConflictError.d.ts +4 -0
  14. package/{src/error/ConflictError.ts → dist/esm/error/ConflictError.js} +1 -2
  15. package/dist/esm/error/ForbiddenError.d.ts +4 -0
  16. package/{src/error/ForbiddenError.ts → dist/esm/error/ForbiddenError.js} +1 -2
  17. package/dist/esm/error/HTTPError.d.ts +5 -0
  18. package/{src/error/HTTPError.ts → dist/esm/error/HTTPError.js} +1 -5
  19. package/dist/esm/error/InternalServerError.d.ts +4 -0
  20. package/{src/error/InternalServerError.ts → dist/esm/error/InternalServerError.js} +1 -2
  21. package/dist/esm/error/NotFoundError.d.ts +4 -0
  22. package/{src/error/NotFoundError.ts → dist/esm/error/NotFoundError.js} +1 -2
  23. package/dist/esm/error/UnauthorizedError.d.ts +4 -0
  24. package/{src/error/UnauthorizedError.ts → dist/esm/error/UnauthorizedError.js} +1 -2
  25. package/dist/esm/error/ValidationError.d.ts +4 -0
  26. package/{src/error/ValidationError.ts → dist/esm/error/ValidationError.js} +1 -2
  27. package/{src/index.ts → dist/esm/index.d.ts} +1 -3
  28. package/dist/esm/index.js +10 -0
  29. package/dist/esm/middleware/express/errorConverter.d.ts +2 -0
  30. package/dist/esm/middleware/express/errorConverter.js +8 -0
  31. package/dist/esm/middleware/express/errorHandler.d.ts +2 -0
  32. package/{src/middleware/errorHandler.ts → dist/esm/middleware/express/errorHandler.js} +3 -13
  33. package/dist/esm/middleware/express/index.d.ts +2 -0
  34. package/dist/esm/middleware/express/index.js +2 -0
  35. package/dist/esm/utils/mapAppErrorToResponder.d.ts +3 -0
  36. package/{src/utils/mapAppErrorToResponder.ts → dist/esm/utils/mapAppErrorToResponder.js} +2 -11
  37. package/dist/types/error/AppError.d.ts +6 -0
  38. package/dist/types/error/BadRequestError.d.ts +4 -0
  39. package/dist/types/error/ConflictError.d.ts +4 -0
  40. package/dist/types/error/ForbiddenError.d.ts +4 -0
  41. package/dist/types/error/HTTPError.d.ts +5 -0
  42. package/dist/types/error/InternalServerError.d.ts +4 -0
  43. package/dist/types/error/NotFoundError.d.ts +4 -0
  44. package/dist/types/error/UnauthorizedError.d.ts +4 -0
  45. package/dist/types/error/ValidationError.d.ts +4 -0
  46. package/dist/types/index.d.ts +10 -0
  47. package/dist/types/middleware/express/errorConverter.d.ts +2 -0
  48. package/dist/types/middleware/express/errorHandler.d.ts +2 -0
  49. package/dist/types/middleware/express/index.d.ts +2 -0
  50. package/dist/types/utils/mapAppErrorToResponder.d.ts +3 -0
  51. package/package.json +23 -7
  52. package/dist/middleware/errorHandler.d.ts +0 -2
  53. package/src/middleware/errorConverter.ts +0 -16
  54. package/tsconfig.json +0 -21
  55. /package/dist/{error → cjs/error}/AppError.d.ts +0 -0
  56. /package/dist/{error → cjs/error}/AppError.js +0 -0
  57. /package/dist/{error → cjs/error}/BadRequestError.d.ts +0 -0
  58. /package/dist/{error → cjs/error}/BadRequestError.js +0 -0
  59. /package/dist/{error → cjs/error}/ConflictError.d.ts +0 -0
  60. /package/dist/{error → cjs/error}/ConflictError.js +0 -0
  61. /package/dist/{error → cjs/error}/ForbiddenError.d.ts +0 -0
  62. /package/dist/{error → cjs/error}/ForbiddenError.js +0 -0
  63. /package/dist/{error → cjs/error}/HTTPError.d.ts +0 -0
  64. /package/dist/{error → cjs/error}/HTTPError.js +0 -0
  65. /package/dist/{error → cjs/error}/InternalServerError.d.ts +0 -0
  66. /package/dist/{error → cjs/error}/InternalServerError.js +0 -0
  67. /package/dist/{error → cjs/error}/NotFoundError.d.ts +0 -0
  68. /package/dist/{error → cjs/error}/NotFoundError.js +0 -0
  69. /package/dist/{error → cjs/error}/UnauthorizedError.d.ts +0 -0
  70. /package/dist/{error → cjs/error}/UnauthorizedError.js +0 -0
  71. /package/dist/{error → cjs/error}/ValidationError.d.ts +0 -0
  72. /package/dist/{error → cjs/error}/ValidationError.js +0 -0
  73. /package/dist/{middleware → cjs/middleware/express}/errorConverter.d.ts +0 -0
  74. /package/dist/{utils → cjs/utils}/mapAppErrorToResponder.d.ts +0 -0
  75. /package/dist/{utils → cjs/utils}/mapAppErrorToResponder.js +0 -0
package/README.md ADDED
@@ -0,0 +1,125 @@
1
+ # @naman_deep_singh/errors-utils
2
+
3
+ **Version:** 1.0.1
4
+
5
+ Standardized error classes and Express middleware for consistent error handling with TypeScript.
6
+
7
+ ## 🚀 Features
8
+
9
+ - ✅ **Structured Error Classes** - AppError, HTTPError, ValidationError, etc.
10
+ - ✅ **Express Middleware** - Error converter and handler
11
+ - ✅ **Response Integration** - Works with @naman_deep_singh/response-utils
12
+ - ✅ **TypeScript Support** - Full type safety
13
+ - ✅ **Consistent Responses** - Standardized error format across your API
14
+
15
+ ## 📦 Installation
16
+
17
+ ```bash
18
+ npm install @naman_deep_singh/errors-utils
19
+ ```
20
+
21
+ ## 🔧 Usage
22
+
23
+ ### Error Classes
24
+
25
+ ```typescript
26
+ import {
27
+ AppError,
28
+ BadRequestError,
29
+ UnauthorizedError,
30
+ NotFoundError,
31
+ ValidationError,
32
+ InternalServerError
33
+ } from '@naman_deep_singh/errors-utils';
34
+
35
+ // Basic usage
36
+ throw new BadRequestError('Invalid input data');
37
+ throw new UnauthorizedError('Authentication required');
38
+ throw new NotFoundError('User not found');
39
+ throw new ValidationError('Email is required');
40
+
41
+ // With details
42
+ throw new BadRequestError('Validation failed', {
43
+ fields: ['email', 'password']
44
+ });
45
+ ```
46
+
47
+ ### Express Middleware
48
+
49
+ ```typescript
50
+ import express from 'express';
51
+ import { expressErrorHandler, errorConverter } from '@naman_deep_singh/errors-utils';
52
+
53
+ const app = express();
54
+
55
+ // Convert unknown errors to AppError
56
+ app.use(errorConverter);
57
+
58
+ // Handle all errors consistently
59
+ app.use(expressErrorHandler);
60
+
61
+ // In your routes
62
+ app.post('/users', (req, res) => {
63
+ if (!req.body.email) {
64
+ throw new BadRequestError('Email is required');
65
+ }
66
+ // Error will be caught and formatted automatically
67
+ });
68
+ ```
69
+
70
+ ## 🔗 Integration
71
+
72
+ ### With @naman_deep_singh/server-utils
73
+
74
+ ```typescript
75
+ import { createServer } from '@naman_deep_singh/server-utils';
76
+ import { expressErrorHandler } from '@naman_deep_singh/errors-utils';
77
+
78
+ const server = createServer('My API', '1.0.0');
79
+
80
+ // Replace basic error handler with advanced one
81
+ server.app.use(expressErrorHandler);
82
+ ```
83
+
84
+ ### With @naman_deep_singh/response-utils
85
+
86
+ ```typescript
87
+ import { responderMiddleware } from '@naman_deep_singh/response-utils';
88
+
89
+ server.app.use(responderMiddleware());
90
+ server.app.use(expressErrorHandler); // Uses response-utils for consistent format
91
+ ```
92
+
93
+ ## 📚 Error Classes
94
+
95
+ | Class | Status Code | Use Case |
96
+ |-------|-------------|----------|
97
+ | `AppError` | Custom | Base error class |
98
+ | `BadRequestError` | 400 | Invalid input data |
99
+ | `UnauthorizedError` | 401 | Authentication failures |
100
+ | `ForbiddenError` | 403 | Authorization failures |
101
+ | `NotFoundError` | 404 | Resource not found |
102
+ | `ConflictError` | 409 | Resource conflicts |
103
+ | `ValidationError` | 422 | Input validation errors |
104
+ | `InternalServerError` | 500 | Server errors |
105
+
106
+ ## 🎯 Response Format
107
+
108
+ All errors produce consistent responses:
109
+
110
+ ```json
111
+ {
112
+ "success": false,
113
+ "message": "Error message",
114
+ "data": undefined,
115
+ "error": {
116
+ "message": "Detailed error message",
117
+ "details": {...}
118
+ },
119
+ "meta": null
120
+ }
121
+ ```
122
+
123
+ ## 📄 License
124
+
125
+ ISC © Naman Deep Singh
@@ -7,5 +7,4 @@ export * from "./error/NotFoundError";
7
7
  export * from "./error/ConflictError";
8
8
  export * from "./error/ValidationError";
9
9
  export * from "./error/InternalServerError";
10
- export * from "./middleware/errorConverter";
11
- export * from "./middleware/errorHandler";
10
+ export * from "./middleware/express";
@@ -23,5 +23,4 @@ __exportStar(require("./error/NotFoundError"), exports);
23
23
  __exportStar(require("./error/ConflictError"), exports);
24
24
  __exportStar(require("./error/ValidationError"), exports);
25
25
  __exportStar(require("./error/InternalServerError"), exports);
26
- __exportStar(require("./middleware/errorConverter"), exports);
27
- __exportStar(require("./middleware/errorHandler"), exports);
26
+ __exportStar(require("./middleware/express"), exports);
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.errorConverter = errorConverter;
4
- const AppError_1 = require("../error/AppError");
4
+ const AppError_1 = require("../../error/AppError");
5
5
  function errorConverter(err, req, res, next) {
6
6
  if (err instanceof AppError_1.AppError) {
7
7
  return next(err);
@@ -0,0 +1,2 @@
1
+ import type { Request, Response, NextFunction } from "express";
2
+ export declare function expressErrorHandler(err: unknown, req: Request, res: Response, next: NextFunction): any;
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.errorHandler = errorHandler;
4
- const AppError_1 = require("../error/AppError");
3
+ exports.expressErrorHandler = expressErrorHandler;
4
+ const AppError_1 = require("../../error/AppError");
5
5
  const response_utils_1 = require("@naman_deep_singh/response-utils");
6
- const mapAppErrorToResponder_1 = require("src/utils/mapAppErrorToResponder");
7
- function errorHandler(err, req, res, next) {
6
+ const mapAppErrorToResponder_1 = require("../../utils/mapAppErrorToResponder");
7
+ function expressErrorHandler(err, req, res, next) {
8
8
  const responder = new response_utils_1.ExpressResponder({}, res);
9
9
  // AppError → known operational error
10
10
  if (err instanceof AppError_1.AppError) {
@@ -0,0 +1,2 @@
1
+ export * from './errorHandler';
2
+ export * from './errorConverter';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./errorHandler"), exports);
18
+ __exportStar(require("./errorConverter"), exports);
@@ -0,0 +1,6 @@
1
+ export declare class AppError extends Error {
2
+ statusCode: number;
3
+ isOperational: boolean;
4
+ details?: any;
5
+ constructor(message: string, statusCode?: number, details?: any);
6
+ }
@@ -1,16 +1,9 @@
1
1
  export class AppError extends Error {
2
- public statusCode: number;
3
- public isOperational: boolean;
4
- public details?: any;
5
-
6
- constructor(message: string, statusCode = 500, details?: any) {
2
+ constructor(message, statusCode = 500, details) {
7
3
  super(message);
8
-
9
4
  this.statusCode = statusCode;
10
5
  this.isOperational = true;
11
6
  this.details = details;
12
-
13
7
  Error.captureStackTrace(this, this.constructor);
14
8
  }
15
9
  }
16
-
@@ -0,0 +1,4 @@
1
+ import { HTTPError } from "./HTTPError";
2
+ export declare class BadRequestError extends HTTPError {
3
+ constructor(message?: string, details?: any);
4
+ }
@@ -1,7 +1,6 @@
1
1
  import { HTTPError } from "./HTTPError";
2
-
3
2
  export class BadRequestError extends HTTPError {
4
- constructor(message = "Bad Request", details?: any) {
3
+ constructor(message = "Bad Request", details) {
5
4
  super(message, 400, details);
6
5
  }
7
6
  }
@@ -0,0 +1,4 @@
1
+ import { HTTPError } from "./HTTPError";
2
+ export declare class ConflictError extends HTTPError {
3
+ constructor(message?: string, details?: any);
4
+ }
@@ -1,7 +1,6 @@
1
1
  import { HTTPError } from "./HTTPError";
2
-
3
2
  export class ConflictError extends HTTPError {
4
- constructor(message = "Conflict", details?: any) {
3
+ constructor(message = "Conflict", details) {
5
4
  super(message, 409, details);
6
5
  }
7
6
  }
@@ -0,0 +1,4 @@
1
+ import { HTTPError } from "./HTTPError";
2
+ export declare class ForbiddenError extends HTTPError {
3
+ constructor(message?: string, details?: any);
4
+ }
@@ -1,7 +1,6 @@
1
1
  import { HTTPError } from "./HTTPError";
2
-
3
2
  export class ForbiddenError extends HTTPError {
4
- constructor(message = "Forbidden", details?: any) {
3
+ constructor(message = "Forbidden", details) {
5
4
  super(message, 403, details);
6
5
  }
7
6
  }
@@ -0,0 +1,5 @@
1
+ import { AppError } from "./AppError";
2
+ export declare class HTTPError extends AppError {
3
+ status: string;
4
+ constructor(message: string, statusCode: number, details?: any);
5
+ }
@@ -1,11 +1,7 @@
1
1
  import { AppError } from "./AppError";
2
-
3
2
  export class HTTPError extends AppError {
4
- public status: string;
5
-
6
- constructor(message: string, statusCode: number, details?: any) {
3
+ constructor(message, statusCode, details) {
7
4
  super(message, statusCode, details);
8
-
9
5
  this.status = `${statusCode}`.startsWith("4") ? "fail" : "error";
10
6
  }
11
7
  }
@@ -0,0 +1,4 @@
1
+ import { HTTPError } from "./HTTPError";
2
+ export declare class InternalServerError extends HTTPError {
3
+ constructor(message?: string, details?: any);
4
+ }
@@ -1,7 +1,6 @@
1
1
  import { HTTPError } from "./HTTPError";
2
-
3
2
  export class InternalServerError extends HTTPError {
4
- constructor(message = "Internal Server Error", details?: any) {
3
+ constructor(message = "Internal Server Error", details) {
5
4
  super(message, 500, details);
6
5
  }
7
6
  }
@@ -0,0 +1,4 @@
1
+ import { HTTPError } from "./HTTPError";
2
+ export declare class NotFoundError extends HTTPError {
3
+ constructor(message?: string, details?: any);
4
+ }
@@ -1,7 +1,6 @@
1
1
  import { HTTPError } from "./HTTPError";
2
-
3
2
  export class NotFoundError extends HTTPError {
4
- constructor(message = "Not Found", details?: any) {
3
+ constructor(message = "Not Found", details) {
5
4
  super(message, 404, details);
6
5
  }
7
6
  }
@@ -0,0 +1,4 @@
1
+ import { HTTPError } from "./HTTPError";
2
+ export declare class UnauthorizedError extends HTTPError {
3
+ constructor(message?: string, details?: any);
4
+ }
@@ -1,7 +1,6 @@
1
1
  import { HTTPError } from "./HTTPError";
2
-
3
2
  export class UnauthorizedError extends HTTPError {
4
- constructor(message = "Unauthorized", details?: any) {
3
+ constructor(message = "Unauthorized", details) {
5
4
  super(message, 401, details);
6
5
  }
7
6
  }
@@ -0,0 +1,4 @@
1
+ import { HTTPError } from "./HTTPError";
2
+ export declare class ValidationError extends HTTPError {
3
+ constructor(message?: string, details?: any);
4
+ }
@@ -1,7 +1,6 @@
1
1
  import { HTTPError } from "./HTTPError";
2
-
3
2
  export class ValidationError extends HTTPError {
4
- constructor(message = "Validation Error", details?: any) {
3
+ constructor(message = "Validation Error", details) {
5
4
  super(message, 422, details);
6
5
  }
7
6
  }
@@ -7,6 +7,4 @@ export * from "./error/NotFoundError";
7
7
  export * from "./error/ConflictError";
8
8
  export * from "./error/ValidationError";
9
9
  export * from "./error/InternalServerError";
10
-
11
- export * from "./middleware/errorConverter";
12
- export * from "./middleware/errorHandler";
10
+ export * from "./middleware/express";
@@ -0,0 +1,10 @@
1
+ export * from "./error/AppError";
2
+ export * from "./error/HTTPError";
3
+ export * from "./error/BadRequestError";
4
+ export * from "./error/UnauthorizedError";
5
+ export * from "./error/ForbiddenError";
6
+ export * from "./error/NotFoundError";
7
+ export * from "./error/ConflictError";
8
+ export * from "./error/ValidationError";
9
+ export * from "./error/InternalServerError";
10
+ export * from "./middleware/express";
@@ -0,0 +1,2 @@
1
+ import { Request, Response, NextFunction } from "express";
2
+ export declare function errorConverter(err: any, req: Request, res: Response, next: NextFunction): void;
@@ -0,0 +1,8 @@
1
+ import { AppError } from "../../error/AppError";
2
+ export function errorConverter(err, req, res, next) {
3
+ if (err instanceof AppError) {
4
+ return next(err);
5
+ }
6
+ const convertedError = new AppError(err.message || "Internal Error", err.statusCode || 500, err.details || undefined);
7
+ next(convertedError);
8
+ }
@@ -0,0 +1,2 @@
1
+ import type { Request, Response, NextFunction } from "express";
2
+ export declare function expressErrorHandler(err: unknown, req: Request, res: Response, next: NextFunction): any;
@@ -1,24 +1,14 @@
1
- import type { Request, Response, NextFunction } from "express";
2
- import { AppError } from "../error/AppError";
1
+ import { AppError } from "../../error/AppError";
3
2
  import { ExpressResponder } from "@naman_deep_singh/response-utils";
4
- import { mapAppErrorToResponder } from "src/utils/mapAppErrorToResponder";
5
-
6
- export function errorHandler(
7
- err: unknown,
8
- req: Request,
9
- res: Response,
10
- next: NextFunction
11
- ) {
3
+ import { mapAppErrorToResponder } from "../../utils/mapAppErrorToResponder";
4
+ export function expressErrorHandler(err, req, res, next) {
12
5
  const responder = new ExpressResponder({}, res);
13
-
14
6
  // AppError → known operational error
15
7
  if (err instanceof AppError) {
16
8
  return mapAppErrorToResponder(responder, err);
17
9
  }
18
-
19
10
  // Unexpected / programming / unknown error
20
11
  console.error("UNEXPECTED ERROR:", err);
21
-
22
12
  return responder.serverError("Internal server error", {
23
13
  details: process.env.NODE_ENV === "production" ? undefined : err,
24
14
  });
@@ -0,0 +1,2 @@
1
+ export * from './errorHandler';
2
+ export * from './errorConverter';
@@ -0,0 +1,2 @@
1
+ export * from './errorHandler';
2
+ export * from './errorConverter';
@@ -0,0 +1,3 @@
1
+ import { ExpressResponder } from "@naman_deep_singh/response-utils";
2
+ import { AppError } from "src/error/AppError";
3
+ export declare function mapAppErrorToResponder(responder: ExpressResponder<any>, err: AppError): any;
@@ -1,29 +1,20 @@
1
- import { ExpressResponder, HTTP_STATUS } from "@naman_deep_singh/response-utils";
2
- import { AppError } from "src/error/AppError";
3
-
4
- export function mapAppErrorToResponder(responder: ExpressResponder<any>, err: AppError) {
1
+ import { HTTP_STATUS } from "@naman_deep_singh/response-utils";
2
+ export function mapAppErrorToResponder(responder, err) {
5
3
  switch (err.statusCode) {
6
4
  case HTTP_STATUS.CLIENT_ERROR.BAD_REQUEST:
7
5
  return responder.badRequest(err.message, { details: err.details });
8
-
9
6
  case HTTP_STATUS.CLIENT_ERROR.UNAUTHORIZED:
10
7
  return responder.unauthorized(err.message);
11
-
12
8
  case HTTP_STATUS.CLIENT_ERROR.FORBIDDEN:
13
9
  return responder.forbidden(err.message);
14
-
15
10
  case HTTP_STATUS.CLIENT_ERROR.NOT_FOUND:
16
11
  return responder.notFound(err.message);
17
-
18
12
  case HTTP_STATUS.CLIENT_ERROR.CONFLICT:
19
13
  return responder.conflict(err.message);
20
-
21
14
  case HTTP_STATUS.CLIENT_ERROR.UNPROCESSABLE_ENTITY:
22
15
  return responder.unprocessableEntity(err.message, { details: err.details });
23
-
24
16
  case HTTP_STATUS.CLIENT_ERROR.TOO_MANY_REQUESTS:
25
17
  return responder.tooManyRequests(err.message);
26
-
27
18
  default:
28
19
  // Any other custom status maps to a generic server error
29
20
  return responder.serverError(err.message, { details: err.details });
@@ -0,0 +1,6 @@
1
+ export declare class AppError extends Error {
2
+ statusCode: number;
3
+ isOperational: boolean;
4
+ details?: any;
5
+ constructor(message: string, statusCode?: number, details?: any);
6
+ }
@@ -0,0 +1,4 @@
1
+ import { HTTPError } from "./HTTPError";
2
+ export declare class BadRequestError extends HTTPError {
3
+ constructor(message?: string, details?: any);
4
+ }
@@ -0,0 +1,4 @@
1
+ import { HTTPError } from "./HTTPError";
2
+ export declare class ConflictError extends HTTPError {
3
+ constructor(message?: string, details?: any);
4
+ }
@@ -0,0 +1,4 @@
1
+ import { HTTPError } from "./HTTPError";
2
+ export declare class ForbiddenError extends HTTPError {
3
+ constructor(message?: string, details?: any);
4
+ }
@@ -0,0 +1,5 @@
1
+ import { AppError } from "./AppError";
2
+ export declare class HTTPError extends AppError {
3
+ status: string;
4
+ constructor(message: string, statusCode: number, details?: any);
5
+ }
@@ -0,0 +1,4 @@
1
+ import { HTTPError } from "./HTTPError";
2
+ export declare class InternalServerError extends HTTPError {
3
+ constructor(message?: string, details?: any);
4
+ }
@@ -0,0 +1,4 @@
1
+ import { HTTPError } from "./HTTPError";
2
+ export declare class NotFoundError extends HTTPError {
3
+ constructor(message?: string, details?: any);
4
+ }
@@ -0,0 +1,4 @@
1
+ import { HTTPError } from "./HTTPError";
2
+ export declare class UnauthorizedError extends HTTPError {
3
+ constructor(message?: string, details?: any);
4
+ }
@@ -0,0 +1,4 @@
1
+ import { HTTPError } from "./HTTPError";
2
+ export declare class ValidationError extends HTTPError {
3
+ constructor(message?: string, details?: any);
4
+ }
@@ -0,0 +1,10 @@
1
+ export * from "./error/AppError";
2
+ export * from "./error/HTTPError";
3
+ export * from "./error/BadRequestError";
4
+ export * from "./error/UnauthorizedError";
5
+ export * from "./error/ForbiddenError";
6
+ export * from "./error/NotFoundError";
7
+ export * from "./error/ConflictError";
8
+ export * from "./error/ValidationError";
9
+ export * from "./error/InternalServerError";
10
+ export * from "./middleware/express";
@@ -0,0 +1,2 @@
1
+ import { Request, Response, NextFunction } from "express";
2
+ export declare function errorConverter(err: any, req: Request, res: Response, next: NextFunction): void;
@@ -0,0 +1,2 @@
1
+ import type { Request, Response, NextFunction } from "express";
2
+ export declare function expressErrorHandler(err: unknown, req: Request, res: Response, next: NextFunction): any;
@@ -0,0 +1,2 @@
1
+ export * from './errorHandler';
2
+ export * from './errorConverter';
@@ -0,0 +1,3 @@
1
+ import { ExpressResponder } from "@naman_deep_singh/response-utils";
2
+ import { AppError } from "src/error/AppError";
3
+ export declare function mapAppErrorToResponder(responder: ExpressResponder<any>, err: AppError): any;
package/package.json CHANGED
@@ -1,9 +1,22 @@
1
1
  {
2
2
  "name": "@naman_deep_singh/errors-utils",
3
- "version": "1.0.0",
4
- "description": "Security utilities for password hashing and JWT token management with TypeScript",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
3
+ "version": "1.0.1",
4
+ "description": "Standardized error classes and Express middleware for consistent error handling with TypeScript",
5
+ "type": "module",
6
+ "main": "./dist/cjs/index.js",
7
+ "module": "./dist/esm/index.js",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/esm/index.js",
11
+ "require": "./dist/cjs/index.js",
12
+ "types": "./dist/types/index.d.ts"
13
+ }
14
+ },
15
+ "sideEffects": false,
16
+ "files": [
17
+ "dist",
18
+ "README.md"
19
+ ],
7
20
  "keywords": [
8
21
  "errors"
9
22
  ],
@@ -13,12 +26,15 @@
13
26
  "@types/express": "^5.0.5",
14
27
  "@types/node": "^25.0.1",
15
28
  "express": "^5.1.0",
16
- "typescript": "^5.9.3"
29
+ "typescript": "^5.9.3",
30
+ "rimraf": "^5.0.5"
17
31
  },
18
32
  "dependencies": {
19
- "@naman_deep_singh/response-utils": "^2.0.3"
33
+ "@naman_deep_singh/response-utils": "^2.0.4"
20
34
  },
21
35
  "scripts": {
22
- "build": "tsc"
36
+ "build": "pnpm run build:types && tsc -p tsconfig.cjs.json && tsc -p tsconfig.esm.json",
37
+ "build:types": "tsc -p tsconfig.base.json --emitDeclarationOnly --outDir dist/types",
38
+ "clean": "rimraf dist"
23
39
  }
24
40
  }
@@ -1,2 +0,0 @@
1
- import type { Request, Response, NextFunction } from "express";
2
- export declare function errorHandler(err: unknown, req: Request, res: Response, next: NextFunction): any;
@@ -1,16 +0,0 @@
1
- import { Request, Response, NextFunction } from "express";
2
- import { AppError } from "../error/AppError";
3
-
4
- export function errorConverter(err: any, req: Request, res: Response, next: NextFunction) {
5
- if (err instanceof AppError) {
6
- return next(err);
7
- }
8
-
9
- const convertedError = new AppError(
10
- err.message || "Internal Error",
11
- err.statusCode || 500,
12
- err.details || undefined
13
- );
14
-
15
- next(convertedError);
16
- }
package/tsconfig.json DELETED
@@ -1,21 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2020",
4
- "module": "CommonJS",
5
- "moduleResolution": "node",
6
- "rootDir": "./src",
7
- "outDir": "./dist",
8
- "strict": true,
9
- "esModuleInterop": true,
10
- "allowSyntheticDefaultImports": true,
11
- "skipLibCheck": true,
12
- "forceConsistentCasingInFileNames": true,
13
- "declaration": true,
14
- "baseUrl": ".",
15
- "paths": {
16
- "*": ["*", "*.ts", "*.js"]
17
- }
18
- },
19
- "include": ["src/**/*"],
20
- "exclude": ["node_modules", "dist"]
21
- }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes