@hemia/core 0.0.13 → 0.0.15

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.
@@ -1,6 +1,6 @@
1
1
  import 'reflect-metadata';
2
2
  import express, { Router } from 'express';
3
- import { METADATA_KEYS, ParamType, ApiResponse, isRedirectResponse, ControllerRegistry, CustomHttpError } from '@hemia/common';
3
+ import { METADATA_KEYS, ParamType, ApiResponse, isRedirectResponse, HttpError, ControllerRegistry, CustomHttpError } from '@hemia/common';
4
4
  import { TRACE_METADATA_KEY } from '@hemia/trace-manager';
5
5
  import { traceMiddleware } from '@hemia/app-context';
6
6
  import { plainToInstance } from 'class-transformer';
@@ -192,9 +192,17 @@ async function registerRoutes(app, container, controllerIdentifiers, onTraceFini
192
192
  const validators = Reflect.getMetadata(METADATA_KEYS.VALIDATORS, instance, route.methodName) || [];
193
193
  for (const { index, validator } of validators) {
194
194
  const value = args[index];
195
- const result = validator(value);
196
- if (result === false) {
197
- return res.status(400).json(ApiResponse.error('Validation failed', undefined, 400));
195
+ try {
196
+ const result = await Promise.resolve(validator(value));
197
+ if (result === false) {
198
+ return res.status(400).json(ApiResponse.error('Validation failed', undefined, 400));
199
+ }
200
+ }
201
+ catch (error) {
202
+ if (error.name === 'ValidationException') {
203
+ return res.status(400).json(ApiResponse.error('Validation failed', error.toJSON ? error.toJSON() : error.errors, 400));
204
+ }
205
+ throw error;
198
206
  }
199
207
  }
200
208
  // EJECUCIÓN DEL CONTROLLER
@@ -227,7 +235,9 @@ async function registerRoutes(app, container, controllerIdentifiers, onTraceFini
227
235
  console.error(`Error en ${route.method.toUpperCase()} ${basePath}${route.path}:`, error);
228
236
  const status = error.statusCode || error.status || 500;
229
237
  const message = error.message || 'Internal Server Error';
230
- const errorResponse = ApiResponse.error(message, error.stack, status);
238
+ const errorDetail = error instanceof HttpError ? error.error : undefined;
239
+ const isDev = process.env.NODE_ENV === 'development';
240
+ const errorResponse = ApiResponse.error(message, errorDetail || (isDev ? error.stack : undefined), status);
231
241
  const { status: errStatus, ...errorData } = errorResponse;
232
242
  res.status(errStatus).json(errorData);
233
243
  }
@@ -194,9 +194,17 @@ async function registerRoutes(app, container, controllerIdentifiers, onTraceFini
194
194
  const validators = Reflect.getMetadata(common.METADATA_KEYS.VALIDATORS, instance, route.methodName) || [];
195
195
  for (const { index, validator } of validators) {
196
196
  const value = args[index];
197
- const result = validator(value);
198
- if (result === false) {
199
- return res.status(400).json(common.ApiResponse.error('Validation failed', undefined, 400));
197
+ try {
198
+ const result = await Promise.resolve(validator(value));
199
+ if (result === false) {
200
+ return res.status(400).json(common.ApiResponse.error('Validation failed', undefined, 400));
201
+ }
202
+ }
203
+ catch (error) {
204
+ if (error.name === 'ValidationException') {
205
+ return res.status(400).json(common.ApiResponse.error('Validation failed', error.toJSON ? error.toJSON() : error.errors, 400));
206
+ }
207
+ throw error;
200
208
  }
201
209
  }
202
210
  // EJECUCIÓN DEL CONTROLLER
@@ -229,7 +237,9 @@ async function registerRoutes(app, container, controllerIdentifiers, onTraceFini
229
237
  console.error(`Error en ${route.method.toUpperCase()} ${basePath}${route.path}:`, error);
230
238
  const status = error.statusCode || error.status || 500;
231
239
  const message = error.message || 'Internal Server Error';
232
- const errorResponse = common.ApiResponse.error(message, error.stack, status);
240
+ const errorDetail = error instanceof common.HttpError ? error.error : undefined;
241
+ const isDev = process.env.NODE_ENV === 'development';
242
+ const errorResponse = common.ApiResponse.error(message, errorDetail || (isDev ? error.stack : undefined), status);
233
243
  const { status: errStatus, ...errorData } = errorResponse;
234
244
  res.status(errStatus).json(errorData);
235
245
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hemia/core",
3
- "version": "0.0.13",
3
+ "version": "0.0.15",
4
4
  "description": "Core utilities for Hemia projects",
5
5
  "main": "dist/hemia-core.js",
6
6
  "module": "dist/hemia-core.esm.js",
@@ -18,7 +18,7 @@
18
18
  "@rollup/plugin-commonjs": "^26.0.1",
19
19
  "@rollup/plugin-json": "^6.1.0",
20
20
  "@rollup/plugin-node-resolve": "^15.2.3",
21
- "@hemia/common": "^0.0.15",
21
+ "@hemia/common": "^0.0.17",
22
22
  "@hemia/app-context": "^0.0.6",
23
23
  "@hemia/trace-manager": "^0.0.9",
24
24
  "@hemia/auth-sdk": "^0.0.16",