elseware-nodejs 1.8.6 → 1.8.7

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/dist/index.d.cts CHANGED
@@ -1809,10 +1809,10 @@ declare class AppError extends Error {
1809
1809
  status: string;
1810
1810
  isOperational: boolean;
1811
1811
  code?: string;
1812
- details?: unknown;
1812
+ details?: Record<string, unknown>;
1813
1813
  constructor(message: string, statusCode?: number, options?: {
1814
1814
  code?: string;
1815
- details?: unknown;
1815
+ details?: Record<string, unknown>;
1816
1816
  });
1817
1817
  }
1818
1818
 
package/dist/index.d.ts CHANGED
@@ -1809,10 +1809,10 @@ declare class AppError extends Error {
1809
1809
  status: string;
1810
1810
  isOperational: boolean;
1811
1811
  code?: string;
1812
- details?: unknown;
1812
+ details?: Record<string, unknown>;
1813
1813
  constructor(message: string, statusCode?: number, options?: {
1814
1814
  code?: string;
1815
- details?: unknown;
1815
+ details?: Record<string, unknown>;
1816
1816
  });
1817
1817
  }
1818
1818
 
package/dist/index.js CHANGED
@@ -5097,16 +5097,23 @@ var handleCastErrorDB = (err) => new AppError(`Invalid ${err.path}: ${err.value}
5097
5097
  code: "INVALID_ID"
5098
5098
  });
5099
5099
  var handleDuplicateFieldsDB = (err) => {
5100
- const value = err.keyValue ? JSON.stringify(err.keyValue) : "duplicate value";
5101
- return new AppError(`Duplicate field value: ${value}`, 400, {
5100
+ const field = err.keyValue ? Object.keys(err.keyValue)[0] : "field";
5101
+ return new AppError(`${field} already exists`, 400, {
5102
5102
  code: "DUPLICATE_FIELD"
5103
5103
  });
5104
5104
  };
5105
5105
  var handleValidationErrorDB = (err) => {
5106
- const errors = Object.values(err.errors).map((el) => el.message);
5107
- return new AppError("Invalid input data", 400, {
5106
+ const errors = Object.entries(err.errors).reduce(
5107
+ (acc, [key, value]) => {
5108
+ acc[key] = value.message;
5109
+ return acc;
5110
+ },
5111
+ {}
5112
+ );
5113
+ return new AppError("Validation failed", 400, {
5108
5114
  code: "VALIDATION_ERROR",
5109
5115
  details: errors
5116
+ // structured field errors
5110
5117
  });
5111
5118
  };
5112
5119
  var handleJWTError = () => new AppError("Invalid token. Please log in again.", 401);
@@ -5144,21 +5151,27 @@ var GlobalErrorHandler = (isProd = false) => (err, _req, res, _next) => {
5144
5151
  } else {
5145
5152
  logger.danger("Operational error", { message: error.message });
5146
5153
  }
5154
+ const baseResponse = {
5155
+ success: false,
5156
+ message: error.isOperational ? error.message : "Something went wrong",
5157
+ ...error.details && {
5158
+ meta: {
5159
+ errors: error.details
5160
+ }
5161
+ }
5162
+ };
5147
5163
  if (!isProd) {
5148
5164
  return APIResponse.send(res, {
5149
- success: false,
5150
5165
  statusCode: error.statusCode,
5151
- message: error.message,
5166
+ ...baseResponse,
5152
5167
  data: {
5153
- stack: err?.stack,
5154
- details: error.details
5168
+ stack: err?.stack
5155
5169
  }
5156
5170
  });
5157
5171
  }
5158
5172
  return APIResponse.send(res, {
5159
- success: false,
5160
5173
  statusCode: error.statusCode,
5161
- message: error.isOperational ? error.message : "Something went wrong"
5174
+ ...baseResponse
5162
5175
  });
5163
5176
  };
5164
5177