@naman_deep_singh/errors-utils 1.0.1 → 1.1.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 (64) hide show
  1. package/README.md +1 -1
  2. package/dist/cjs/error/AppError.d.ts +2 -2
  3. package/dist/cjs/error/BadRequestError.d.ts +2 -2
  4. package/dist/cjs/error/BadRequestError.js +1 -1
  5. package/dist/cjs/error/ConflictError.d.ts +2 -2
  6. package/dist/cjs/error/ConflictError.js +1 -1
  7. package/dist/cjs/error/ForbiddenError.d.ts +2 -2
  8. package/dist/cjs/error/ForbiddenError.js +1 -1
  9. package/dist/cjs/error/HTTPError.d.ts +2 -2
  10. package/dist/cjs/error/HTTPError.js +1 -1
  11. package/dist/cjs/error/InternalServerError.d.ts +2 -2
  12. package/dist/cjs/error/InternalServerError.js +1 -1
  13. package/dist/cjs/error/NotFoundError.d.ts +2 -2
  14. package/dist/cjs/error/NotFoundError.js +1 -1
  15. package/dist/cjs/error/UnauthorizedError.d.ts +2 -2
  16. package/dist/cjs/error/UnauthorizedError.js +1 -1
  17. package/dist/cjs/error/ValidationError.d.ts +2 -2
  18. package/dist/cjs/error/ValidationError.js +1 -1
  19. package/dist/cjs/index.d.ts +10 -10
  20. package/dist/cjs/middleware/express/errorConverter.d.ts +2 -2
  21. package/dist/cjs/middleware/express/errorConverter.js +17 -1
  22. package/dist/cjs/middleware/express/errorHandler.d.ts +1 -1
  23. package/dist/cjs/middleware/express/errorHandler.js +4 -4
  24. package/dist/cjs/utils/mapAppErrorToResponder.d.ts +3 -3
  25. package/dist/cjs/utils/mapAppErrorToResponder.js +3 -1
  26. package/dist/esm/error/AppError.d.ts +2 -2
  27. package/dist/esm/error/BadRequestError.d.ts +2 -2
  28. package/dist/esm/error/BadRequestError.js +2 -2
  29. package/dist/esm/error/ConflictError.d.ts +2 -2
  30. package/dist/esm/error/ConflictError.js +2 -2
  31. package/dist/esm/error/ForbiddenError.d.ts +2 -2
  32. package/dist/esm/error/ForbiddenError.js +2 -2
  33. package/dist/esm/error/HTTPError.d.ts +2 -2
  34. package/dist/esm/error/HTTPError.js +2 -2
  35. package/dist/esm/error/InternalServerError.d.ts +2 -2
  36. package/dist/esm/error/InternalServerError.js +2 -2
  37. package/dist/esm/error/NotFoundError.d.ts +2 -2
  38. package/dist/esm/error/NotFoundError.js +2 -2
  39. package/dist/esm/error/UnauthorizedError.d.ts +2 -2
  40. package/dist/esm/error/UnauthorizedError.js +2 -2
  41. package/dist/esm/error/ValidationError.d.ts +2 -2
  42. package/dist/esm/error/ValidationError.js +2 -2
  43. package/dist/esm/index.d.ts +10 -10
  44. package/dist/esm/index.js +10 -10
  45. package/dist/esm/middleware/express/errorConverter.d.ts +2 -2
  46. package/dist/esm/middleware/express/errorConverter.js +18 -2
  47. package/dist/esm/middleware/express/errorHandler.d.ts +1 -1
  48. package/dist/esm/middleware/express/errorHandler.js +6 -6
  49. package/dist/esm/utils/mapAppErrorToResponder.d.ts +3 -3
  50. package/dist/esm/utils/mapAppErrorToResponder.js +4 -2
  51. package/dist/types/error/AppError.d.ts +2 -2
  52. package/dist/types/error/BadRequestError.d.ts +2 -2
  53. package/dist/types/error/ConflictError.d.ts +2 -2
  54. package/dist/types/error/ForbiddenError.d.ts +2 -2
  55. package/dist/types/error/HTTPError.d.ts +2 -2
  56. package/dist/types/error/InternalServerError.d.ts +2 -2
  57. package/dist/types/error/NotFoundError.d.ts +2 -2
  58. package/dist/types/error/UnauthorizedError.d.ts +2 -2
  59. package/dist/types/error/ValidationError.d.ts +2 -2
  60. package/dist/types/index.d.ts +10 -10
  61. package/dist/types/middleware/express/errorConverter.d.ts +2 -2
  62. package/dist/types/middleware/express/errorHandler.d.ts +1 -1
  63. package/dist/types/utils/mapAppErrorToResponder.d.ts +3 -3
  64. package/package.json +1 -1
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @naman_deep_singh/errors-utils
2
2
 
3
- **Version:** 1.0.1
3
+ **Version:** 1.1.1
4
4
 
5
5
  Standardized error classes and Express middleware for consistent error handling with TypeScript.
6
6
 
@@ -1,6 +1,6 @@
1
1
  export declare class AppError extends Error {
2
2
  statusCode: number;
3
3
  isOperational: boolean;
4
- details?: any;
5
- constructor(message: string, statusCode?: number, details?: any);
4
+ details?: unknown;
5
+ constructor(message: string, statusCode?: number, details?: unknown);
6
6
  }
@@ -1,4 +1,4 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export declare class BadRequestError extends HTTPError {
3
- constructor(message?: string, details?: any);
3
+ constructor(message?: string, details?: unknown);
4
4
  }
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BadRequestError = void 0;
4
4
  const HTTPError_1 = require("./HTTPError");
5
5
  class BadRequestError extends HTTPError_1.HTTPError {
6
- constructor(message = "Bad Request", details) {
6
+ constructor(message = 'Bad Request', details) {
7
7
  super(message, 400, details);
8
8
  }
9
9
  }
@@ -1,4 +1,4 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export declare class ConflictError extends HTTPError {
3
- constructor(message?: string, details?: any);
3
+ constructor(message?: string, details?: unknown);
4
4
  }
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ConflictError = void 0;
4
4
  const HTTPError_1 = require("./HTTPError");
5
5
  class ConflictError extends HTTPError_1.HTTPError {
6
- constructor(message = "Conflict", details) {
6
+ constructor(message = 'Conflict', details) {
7
7
  super(message, 409, details);
8
8
  }
9
9
  }
@@ -1,4 +1,4 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export declare class ForbiddenError extends HTTPError {
3
- constructor(message?: string, details?: any);
3
+ constructor(message?: string, details?: unknown);
4
4
  }
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ForbiddenError = void 0;
4
4
  const HTTPError_1 = require("./HTTPError");
5
5
  class ForbiddenError extends HTTPError_1.HTTPError {
6
- constructor(message = "Forbidden", details) {
6
+ constructor(message = 'Forbidden', details) {
7
7
  super(message, 403, details);
8
8
  }
9
9
  }
@@ -1,5 +1,5 @@
1
- import { AppError } from "./AppError";
1
+ import { AppError } from './AppError';
2
2
  export declare class HTTPError extends AppError {
3
3
  status: string;
4
- constructor(message: string, statusCode: number, details?: any);
4
+ constructor(message: string, statusCode: number, details?: unknown);
5
5
  }
@@ -5,7 +5,7 @@ const AppError_1 = require("./AppError");
5
5
  class HTTPError extends AppError_1.AppError {
6
6
  constructor(message, statusCode, details) {
7
7
  super(message, statusCode, details);
8
- this.status = `${statusCode}`.startsWith("4") ? "fail" : "error";
8
+ this.status = `${statusCode}`.startsWith('4') ? 'fail' : 'error';
9
9
  }
10
10
  }
11
11
  exports.HTTPError = HTTPError;
@@ -1,4 +1,4 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export declare class InternalServerError extends HTTPError {
3
- constructor(message?: string, details?: any);
3
+ constructor(message?: string, details?: unknown);
4
4
  }
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.InternalServerError = void 0;
4
4
  const HTTPError_1 = require("./HTTPError");
5
5
  class InternalServerError extends HTTPError_1.HTTPError {
6
- constructor(message = "Internal Server Error", details) {
6
+ constructor(message = 'Internal Server Error', details) {
7
7
  super(message, 500, details);
8
8
  }
9
9
  }
@@ -1,4 +1,4 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export declare class NotFoundError extends HTTPError {
3
- constructor(message?: string, details?: any);
3
+ constructor(message?: string, details?: unknown);
4
4
  }
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NotFoundError = void 0;
4
4
  const HTTPError_1 = require("./HTTPError");
5
5
  class NotFoundError extends HTTPError_1.HTTPError {
6
- constructor(message = "Not Found", details) {
6
+ constructor(message = 'Not Found', details) {
7
7
  super(message, 404, details);
8
8
  }
9
9
  }
@@ -1,4 +1,4 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export declare class UnauthorizedError extends HTTPError {
3
- constructor(message?: string, details?: any);
3
+ constructor(message?: string, details?: unknown);
4
4
  }
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.UnauthorizedError = void 0;
4
4
  const HTTPError_1 = require("./HTTPError");
5
5
  class UnauthorizedError extends HTTPError_1.HTTPError {
6
- constructor(message = "Unauthorized", details) {
6
+ constructor(message = 'Unauthorized', details) {
7
7
  super(message, 401, details);
8
8
  }
9
9
  }
@@ -1,4 +1,4 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export declare class ValidationError extends HTTPError {
3
- constructor(message?: string, details?: any);
3
+ constructor(message?: string, details?: unknown);
4
4
  }
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ValidationError = void 0;
4
4
  const HTTPError_1 = require("./HTTPError");
5
5
  class ValidationError extends HTTPError_1.HTTPError {
6
- constructor(message = "Validation Error", details) {
6
+ constructor(message = 'Validation Error', details) {
7
7
  super(message, 422, details);
8
8
  }
9
9
  }
@@ -1,10 +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";
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';
@@ -1,2 +1,2 @@
1
- import { Request, Response, NextFunction } from "express";
2
- export declare function errorConverter(err: any, req: Request, res: Response, next: NextFunction): void;
1
+ import type { NextFunction, Request, Response } from 'express';
2
+ export declare function errorConverter(err: unknown, req: Request, res: Response, next: NextFunction): void;
@@ -6,6 +6,22 @@ function errorConverter(err, req, res, next) {
6
6
  if (err instanceof AppError_1.AppError) {
7
7
  return next(err);
8
8
  }
9
- const convertedError = new AppError_1.AppError(err.message || "Internal Error", err.statusCode || 500, err.details || undefined);
9
+ // Safely extract properties from unknown error
10
+ let message = 'Internal Error';
11
+ let statusCode = 500;
12
+ let details = undefined;
13
+ if (typeof err === 'object' && err !== null) {
14
+ const e = err;
15
+ if (typeof e.message === 'string')
16
+ message = e.message;
17
+ if (typeof e.statusCode === 'number')
18
+ statusCode = e.statusCode;
19
+ if ('details' in e)
20
+ details = e.details;
21
+ }
22
+ else if (typeof err === 'string') {
23
+ message = err;
24
+ }
25
+ const convertedError = new AppError_1.AppError(message, statusCode, details);
10
26
  next(convertedError);
11
27
  }
@@ -1,2 +1,2 @@
1
- import type { Request, Response, NextFunction } from "express";
1
+ import type { NextFunction, Request, Response } from 'express';
2
2
  export declare function expressErrorHandler(err: unknown, req: Request, res: Response, next: NextFunction): any;
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.expressErrorHandler = expressErrorHandler;
4
- const AppError_1 = require("../../error/AppError");
5
4
  const response_utils_1 = require("@naman_deep_singh/response-utils");
5
+ const AppError_1 = require("../../error/AppError");
6
6
  const mapAppErrorToResponder_1 = require("../../utils/mapAppErrorToResponder");
7
7
  function expressErrorHandler(err, req, res, next) {
8
8
  const responder = new response_utils_1.ExpressResponder({}, res);
@@ -11,8 +11,8 @@ function expressErrorHandler(err, req, res, next) {
11
11
  return (0, mapAppErrorToResponder_1.mapAppErrorToResponder)(responder, err);
12
12
  }
13
13
  // Unexpected / programming / unknown error
14
- console.error("UNEXPECTED ERROR:", err);
15
- return responder.serverError("Internal server error", {
16
- details: process.env.NODE_ENV === "production" ? undefined : err,
14
+ console.error('UNEXPECTED ERROR:', err);
15
+ return responder.serverError('Internal server error', {
16
+ details: process.env.NODE_ENV === 'production' ? undefined : err,
17
17
  });
18
18
  }
@@ -1,3 +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
+ import type { ExpressResponder } from '@naman_deep_singh/response-utils';
2
+ import type { AppError } from 'src/error/AppError';
3
+ export declare function mapAppErrorToResponder(responder: ExpressResponder<unknown>, err: AppError): any;
@@ -15,7 +15,9 @@ function mapAppErrorToResponder(responder, err) {
15
15
  case response_utils_1.HTTP_STATUS.CLIENT_ERROR.CONFLICT:
16
16
  return responder.conflict(err.message);
17
17
  case response_utils_1.HTTP_STATUS.CLIENT_ERROR.UNPROCESSABLE_ENTITY:
18
- return responder.unprocessableEntity(err.message, { details: err.details });
18
+ return responder.unprocessableEntity(err.message, {
19
+ details: err.details,
20
+ });
19
21
  case response_utils_1.HTTP_STATUS.CLIENT_ERROR.TOO_MANY_REQUESTS:
20
22
  return responder.tooManyRequests(err.message);
21
23
  default:
@@ -1,6 +1,6 @@
1
1
  export declare class AppError extends Error {
2
2
  statusCode: number;
3
3
  isOperational: boolean;
4
- details?: any;
5
- constructor(message: string, statusCode?: number, details?: any);
4
+ details?: unknown;
5
+ constructor(message: string, statusCode?: number, details?: unknown);
6
6
  }
@@ -1,4 +1,4 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export declare class BadRequestError extends HTTPError {
3
- constructor(message?: string, details?: any);
3
+ constructor(message?: string, details?: unknown);
4
4
  }
@@ -1,6 +1,6 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export class BadRequestError extends HTTPError {
3
- constructor(message = "Bad Request", details) {
3
+ constructor(message = 'Bad Request', details) {
4
4
  super(message, 400, details);
5
5
  }
6
6
  }
@@ -1,4 +1,4 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export declare class ConflictError extends HTTPError {
3
- constructor(message?: string, details?: any);
3
+ constructor(message?: string, details?: unknown);
4
4
  }
@@ -1,6 +1,6 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export class ConflictError extends HTTPError {
3
- constructor(message = "Conflict", details) {
3
+ constructor(message = 'Conflict', details) {
4
4
  super(message, 409, details);
5
5
  }
6
6
  }
@@ -1,4 +1,4 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export declare class ForbiddenError extends HTTPError {
3
- constructor(message?: string, details?: any);
3
+ constructor(message?: string, details?: unknown);
4
4
  }
@@ -1,6 +1,6 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export class ForbiddenError extends HTTPError {
3
- constructor(message = "Forbidden", details) {
3
+ constructor(message = 'Forbidden', details) {
4
4
  super(message, 403, details);
5
5
  }
6
6
  }
@@ -1,5 +1,5 @@
1
- import { AppError } from "./AppError";
1
+ import { AppError } from './AppError';
2
2
  export declare class HTTPError extends AppError {
3
3
  status: string;
4
- constructor(message: string, statusCode: number, details?: any);
4
+ constructor(message: string, statusCode: number, details?: unknown);
5
5
  }
@@ -1,7 +1,7 @@
1
- import { AppError } from "./AppError";
1
+ import { AppError } from './AppError';
2
2
  export class HTTPError extends AppError {
3
3
  constructor(message, statusCode, details) {
4
4
  super(message, statusCode, details);
5
- this.status = `${statusCode}`.startsWith("4") ? "fail" : "error";
5
+ this.status = `${statusCode}`.startsWith('4') ? 'fail' : 'error';
6
6
  }
7
7
  }
@@ -1,4 +1,4 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export declare class InternalServerError extends HTTPError {
3
- constructor(message?: string, details?: any);
3
+ constructor(message?: string, details?: unknown);
4
4
  }
@@ -1,6 +1,6 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export class InternalServerError extends HTTPError {
3
- constructor(message = "Internal Server Error", details) {
3
+ constructor(message = 'Internal Server Error', details) {
4
4
  super(message, 500, details);
5
5
  }
6
6
  }
@@ -1,4 +1,4 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export declare class NotFoundError extends HTTPError {
3
- constructor(message?: string, details?: any);
3
+ constructor(message?: string, details?: unknown);
4
4
  }
@@ -1,6 +1,6 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export class NotFoundError extends HTTPError {
3
- constructor(message = "Not Found", details) {
3
+ constructor(message = 'Not Found', details) {
4
4
  super(message, 404, details);
5
5
  }
6
6
  }
@@ -1,4 +1,4 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export declare class UnauthorizedError extends HTTPError {
3
- constructor(message?: string, details?: any);
3
+ constructor(message?: string, details?: unknown);
4
4
  }
@@ -1,6 +1,6 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export class UnauthorizedError extends HTTPError {
3
- constructor(message = "Unauthorized", details) {
3
+ constructor(message = 'Unauthorized', details) {
4
4
  super(message, 401, details);
5
5
  }
6
6
  }
@@ -1,4 +1,4 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export declare class ValidationError extends HTTPError {
3
- constructor(message?: string, details?: any);
3
+ constructor(message?: string, details?: unknown);
4
4
  }
@@ -1,6 +1,6 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export class ValidationError extends HTTPError {
3
- constructor(message = "Validation Error", details) {
3
+ constructor(message = 'Validation Error', details) {
4
4
  super(message, 422, details);
5
5
  }
6
6
  }
@@ -1,10 +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";
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';
package/dist/esm/index.js CHANGED
@@ -1,10 +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";
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';
@@ -1,2 +1,2 @@
1
- import { Request, Response, NextFunction } from "express";
2
- export declare function errorConverter(err: any, req: Request, res: Response, next: NextFunction): void;
1
+ import type { NextFunction, Request, Response } from 'express';
2
+ export declare function errorConverter(err: unknown, req: Request, res: Response, next: NextFunction): void;
@@ -1,8 +1,24 @@
1
- import { AppError } from "../../error/AppError";
1
+ import { AppError } from '../../error/AppError';
2
2
  export function errorConverter(err, req, res, next) {
3
3
  if (err instanceof AppError) {
4
4
  return next(err);
5
5
  }
6
- const convertedError = new AppError(err.message || "Internal Error", err.statusCode || 500, err.details || undefined);
6
+ // Safely extract properties from unknown error
7
+ let message = 'Internal Error';
8
+ let statusCode = 500;
9
+ let details = undefined;
10
+ if (typeof err === 'object' && err !== null) {
11
+ const e = err;
12
+ if (typeof e.message === 'string')
13
+ message = e.message;
14
+ if (typeof e.statusCode === 'number')
15
+ statusCode = e.statusCode;
16
+ if ('details' in e)
17
+ details = e.details;
18
+ }
19
+ else if (typeof err === 'string') {
20
+ message = err;
21
+ }
22
+ const convertedError = new AppError(message, statusCode, details);
7
23
  next(convertedError);
8
24
  }
@@ -1,2 +1,2 @@
1
- import type { Request, Response, NextFunction } from "express";
1
+ import type { NextFunction, Request, Response } from 'express';
2
2
  export declare function expressErrorHandler(err: unknown, req: Request, res: Response, next: NextFunction): any;
@@ -1,6 +1,6 @@
1
- import { AppError } from "../../error/AppError";
2
- import { ExpressResponder } from "@naman_deep_singh/response-utils";
3
- import { mapAppErrorToResponder } from "../../utils/mapAppErrorToResponder";
1
+ import { ExpressResponder } from '@naman_deep_singh/response-utils';
2
+ import { AppError } from '../../error/AppError';
3
+ import { mapAppErrorToResponder } from '../../utils/mapAppErrorToResponder';
4
4
  export function expressErrorHandler(err, req, res, next) {
5
5
  const responder = new ExpressResponder({}, res);
6
6
  // AppError → known operational error
@@ -8,8 +8,8 @@ export function expressErrorHandler(err, req, res, next) {
8
8
  return mapAppErrorToResponder(responder, err);
9
9
  }
10
10
  // Unexpected / programming / unknown error
11
- console.error("UNEXPECTED ERROR:", err);
12
- return responder.serverError("Internal server error", {
13
- details: process.env.NODE_ENV === "production" ? undefined : err,
11
+ console.error('UNEXPECTED ERROR:', err);
12
+ return responder.serverError('Internal server error', {
13
+ details: process.env.NODE_ENV === 'production' ? undefined : err,
14
14
  });
15
15
  }
@@ -1,3 +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
+ import type { ExpressResponder } from '@naman_deep_singh/response-utils';
2
+ import type { AppError } from 'src/error/AppError';
3
+ export declare function mapAppErrorToResponder(responder: ExpressResponder<unknown>, err: AppError): any;
@@ -1,4 +1,4 @@
1
- import { HTTP_STATUS } from "@naman_deep_singh/response-utils";
1
+ import { HTTP_STATUS } from '@naman_deep_singh/response-utils';
2
2
  export function mapAppErrorToResponder(responder, err) {
3
3
  switch (err.statusCode) {
4
4
  case HTTP_STATUS.CLIENT_ERROR.BAD_REQUEST:
@@ -12,7 +12,9 @@ export function mapAppErrorToResponder(responder, err) {
12
12
  case HTTP_STATUS.CLIENT_ERROR.CONFLICT:
13
13
  return responder.conflict(err.message);
14
14
  case HTTP_STATUS.CLIENT_ERROR.UNPROCESSABLE_ENTITY:
15
- return responder.unprocessableEntity(err.message, { details: err.details });
15
+ return responder.unprocessableEntity(err.message, {
16
+ details: err.details,
17
+ });
16
18
  case HTTP_STATUS.CLIENT_ERROR.TOO_MANY_REQUESTS:
17
19
  return responder.tooManyRequests(err.message);
18
20
  default:
@@ -1,6 +1,6 @@
1
1
  export declare class AppError extends Error {
2
2
  statusCode: number;
3
3
  isOperational: boolean;
4
- details?: any;
5
- constructor(message: string, statusCode?: number, details?: any);
4
+ details?: unknown;
5
+ constructor(message: string, statusCode?: number, details?: unknown);
6
6
  }
@@ -1,4 +1,4 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export declare class BadRequestError extends HTTPError {
3
- constructor(message?: string, details?: any);
3
+ constructor(message?: string, details?: unknown);
4
4
  }
@@ -1,4 +1,4 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export declare class ConflictError extends HTTPError {
3
- constructor(message?: string, details?: any);
3
+ constructor(message?: string, details?: unknown);
4
4
  }
@@ -1,4 +1,4 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export declare class ForbiddenError extends HTTPError {
3
- constructor(message?: string, details?: any);
3
+ constructor(message?: string, details?: unknown);
4
4
  }
@@ -1,5 +1,5 @@
1
- import { AppError } from "./AppError";
1
+ import { AppError } from './AppError';
2
2
  export declare class HTTPError extends AppError {
3
3
  status: string;
4
- constructor(message: string, statusCode: number, details?: any);
4
+ constructor(message: string, statusCode: number, details?: unknown);
5
5
  }
@@ -1,4 +1,4 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export declare class InternalServerError extends HTTPError {
3
- constructor(message?: string, details?: any);
3
+ constructor(message?: string, details?: unknown);
4
4
  }
@@ -1,4 +1,4 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export declare class NotFoundError extends HTTPError {
3
- constructor(message?: string, details?: any);
3
+ constructor(message?: string, details?: unknown);
4
4
  }
@@ -1,4 +1,4 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export declare class UnauthorizedError extends HTTPError {
3
- constructor(message?: string, details?: any);
3
+ constructor(message?: string, details?: unknown);
4
4
  }
@@ -1,4 +1,4 @@
1
- import { HTTPError } from "./HTTPError";
1
+ import { HTTPError } from './HTTPError';
2
2
  export declare class ValidationError extends HTTPError {
3
- constructor(message?: string, details?: any);
3
+ constructor(message?: string, details?: unknown);
4
4
  }
@@ -1,10 +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";
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';
@@ -1,2 +1,2 @@
1
- import { Request, Response, NextFunction } from "express";
2
- export declare function errorConverter(err: any, req: Request, res: Response, next: NextFunction): void;
1
+ import type { NextFunction, Request, Response } from 'express';
2
+ export declare function errorConverter(err: unknown, req: Request, res: Response, next: NextFunction): void;
@@ -1,2 +1,2 @@
1
- import type { Request, Response, NextFunction } from "express";
1
+ import type { NextFunction, Request, Response } from 'express';
2
2
  export declare function expressErrorHandler(err: unknown, req: Request, res: Response, next: NextFunction): any;
@@ -1,3 +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
+ import type { ExpressResponder } from '@naman_deep_singh/response-utils';
2
+ import type { AppError } from 'src/error/AppError';
3
+ export declare function mapAppErrorToResponder(responder: ExpressResponder<unknown>, err: AppError): any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naman_deep_singh/errors-utils",
3
- "version": "1.0.1",
3
+ "version": "1.1.1",
4
4
  "description": "Standardized error classes and Express middleware for consistent error handling with TypeScript",
5
5
  "type": "module",
6
6
  "main": "./dist/cjs/index.js",