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.cjs CHANGED
@@ -5112,16 +5112,23 @@ var handleCastErrorDB = (err) => new AppError(`Invalid ${err.path}: ${err.value}
5112
5112
  code: "INVALID_ID"
5113
5113
  });
5114
5114
  var handleDuplicateFieldsDB = (err) => {
5115
- const value = err.keyValue ? JSON.stringify(err.keyValue) : "duplicate value";
5116
- return new AppError(`Duplicate field value: ${value}`, 400, {
5115
+ const field = err.keyValue ? Object.keys(err.keyValue)[0] : "field";
5116
+ return new AppError(`${field} already exists`, 400, {
5117
5117
  code: "DUPLICATE_FIELD"
5118
5118
  });
5119
5119
  };
5120
5120
  var handleValidationErrorDB = (err) => {
5121
- const errors = Object.values(err.errors).map((el) => el.message);
5122
- return new AppError("Invalid input data", 400, {
5121
+ const errors = Object.entries(err.errors).reduce(
5122
+ (acc, [key, value]) => {
5123
+ acc[key] = value.message;
5124
+ return acc;
5125
+ },
5126
+ {}
5127
+ );
5128
+ return new AppError("Validation failed", 400, {
5123
5129
  code: "VALIDATION_ERROR",
5124
5130
  details: errors
5131
+ // structured field errors
5125
5132
  });
5126
5133
  };
5127
5134
  var handleJWTError = () => new AppError("Invalid token. Please log in again.", 401);
@@ -5159,21 +5166,27 @@ var GlobalErrorHandler = (isProd = false) => (err, _req, res, _next) => {
5159
5166
  } else {
5160
5167
  logger.danger("Operational error", { message: error.message });
5161
5168
  }
5169
+ const baseResponse = {
5170
+ success: false,
5171
+ message: error.isOperational ? error.message : "Something went wrong",
5172
+ ...error.details && {
5173
+ meta: {
5174
+ errors: error.details
5175
+ }
5176
+ }
5177
+ };
5162
5178
  if (!isProd) {
5163
5179
  return APIResponse.send(res, {
5164
- success: false,
5165
5180
  statusCode: error.statusCode,
5166
- message: error.message,
5181
+ ...baseResponse,
5167
5182
  data: {
5168
- stack: err?.stack,
5169
- details: error.details
5183
+ stack: err?.stack
5170
5184
  }
5171
5185
  });
5172
5186
  }
5173
5187
  return APIResponse.send(res, {
5174
- success: false,
5175
5188
  statusCode: error.statusCode,
5176
- message: error.isOperational ? error.message : "Something went wrong"
5189
+ ...baseResponse
5177
5190
  });
5178
5191
  };
5179
5192