@asapjs/error 1.0.0-alpha.0 → 1.0.0-alpha.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.
package/dist/factory.js CHANGED
@@ -6,7 +6,8 @@ exports.markSwaggerAsReady = markSwaggerAsReady;
6
6
  // addScheme will be injected at runtime
7
7
  let addSchemeFunc = () => { };
8
8
  const types_1 = require("./types");
9
- const schema_1 = require("./schema");
9
+ const schema_1 = require("@asapjs/schema");
10
+ const schema_2 = require("./schema");
10
11
  // 지연 등록을 위한 큐
11
12
  const pendingSchemas = [];
12
13
  let isSwaggerReady = false;
@@ -23,11 +24,11 @@ function error(status, code, message, schema) {
23
24
  const creator = ((data) => {
24
25
  // 스키마 등록 확인 (첫 사용 시)
25
26
  ensureSchemaRegistered(code, status, message, schema);
26
- // 데이터 유효성 검사
27
- (0, schema_1.validateDataWithSchema)(data, schema);
27
+ // 스키마로 데이터만 매핑 (유효성 검사 없음, 예: "1" → 1)
28
+ const mappedData = (0, schema_1.mapDataWithSchema)(data && typeof data === 'object' ? data : {}, schema);
28
29
  // 메시지 템플릿 처리 (예: "사용자 {userId}를 찾을 수 없습니다")
29
- const finalMessage = interpolateMessage(message, data);
30
- return new types_1.HttpError(status, code, finalMessage, data);
30
+ const finalMessage = interpolateMessage(message, mappedData);
31
+ return new types_1.HttpError(status, code, finalMessage, mappedData);
31
32
  });
32
33
  // 메타데이터 저장 (Swagger 문서화용)
33
34
  creator._status = status;
@@ -69,7 +70,7 @@ function registerErrorSwaggerSchema(code, status, message, schema) {
69
70
  data: {
70
71
  type: 'object',
71
72
  properties: Object.entries(schema).reduce((acc, [key, typeIs]) => {
72
- acc[key] = (0, schema_1.typeIsToSwaggerSchema)(typeIs);
73
+ acc[key] = (0, schema_2.typeIsToSwaggerSchema)(typeIs);
73
74
  return acc;
74
75
  }, {}),
75
76
  },
@@ -56,18 +56,40 @@ const effectErrorHandler = (error, req, res, next) => {
56
56
  exports.effectErrorHandler = effectErrorHandler;
57
57
  function wrapWithEffect(handler) {
58
58
  return async (...args) => {
59
- const [req, res, next] = args;
59
+ var _a, _b, _c, _d;
60
+ // asapjs/router Wrapper는 cb(args)로 호출 → 인자 1개가 ExecuteArgs
61
+ const isExecuteArgs = args.length === 1 &&
62
+ args[0] != null &&
63
+ typeof args[0] === 'object' &&
64
+ 'req' in args[0] &&
65
+ 'res' in args[0];
66
+ const req = isExecuteArgs
67
+ ? args[0].req
68
+ : args[0];
69
+ const next = isExecuteArgs
70
+ ? undefined
71
+ : args[2];
60
72
  try {
61
73
  const effect = effect_1.Effect.promise(() => handler(...args));
62
- const traced = effect_1.Effect.withSpan(effect, `${req.method} ${req.path}`, {
74
+ const traced = effect_1.Effect.withSpan(effect, `${(_a = req === null || req === void 0 ? void 0 : req.method) !== null && _a !== void 0 ? _a : 'unknown'} ${(_b = req === null || req === void 0 ? void 0 : req.path) !== null && _b !== void 0 ? _b : ''}`, {
63
75
  attributes: {
64
- 'http.method': req.method,
65
- 'http.path': req.path,
66
- 'http.request_id': req.id || 'unknown',
76
+ 'http.method': (_c = req === null || req === void 0 ? void 0 : req.method) !== null && _c !== void 0 ? _c : 'unknown',
77
+ 'http.path': (_d = req === null || req === void 0 ? void 0 : req.path) !== null && _d !== void 0 ? _d : '',
78
+ 'http.request_id': (req === null || req === void 0 ? void 0 : req.id) || 'unknown',
67
79
  },
68
80
  });
69
- const result = await effect_1.Effect.runPromise(traced);
70
- return result;
81
+ const exit = await effect_1.Effect.runPromiseExit(traced);
82
+ if (effect_1.Exit.isFailure(exit)) {
83
+ const originalError = (0, helpers_1.causeToError)(exit.cause);
84
+ if (next) {
85
+ next(originalError);
86
+ }
87
+ else {
88
+ throw originalError;
89
+ }
90
+ return;
91
+ }
92
+ return exit.value;
71
93
  }
72
94
  catch (error) {
73
95
  if (next) {
package/dist/schema.d.ts CHANGED
@@ -1,3 +1,5 @@
1
- import { SchemaType } from '@asapjs/schema';
2
- export declare function typeIsToSwaggerSchema(typeIs: SchemaType): any;
3
- export declare function validateDataWithSchema(data: any, schema: Record<string, any>): void;
1
+ import type { SchemaType } from '@asapjs/schema';
2
+ /**
3
+ * Swagger 스키마 생성용. error 패키지 내부에서만 사용.
4
+ */
5
+ export declare function typeIsToSwaggerSchema(typeIs: SchemaType | (() => SchemaType)): any;
package/dist/schema.js CHANGED
@@ -1,38 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.typeIsToSwaggerSchema = typeIsToSwaggerSchema;
4
- exports.validateDataWithSchema = validateDataWithSchema;
4
+ /**
5
+ * Swagger 스키마 생성용. error 패키지 내부에서만 사용.
6
+ */
5
7
  function typeIsToSwaggerSchema(typeIs) {
6
- // @asapjs/schema의 toSwagger 메서드 사용
7
- if (typeIs.toSwagger) {
8
- return typeIs.toSwagger();
8
+ const schemaType = typeof typeIs === 'function' ? typeIs() : typeIs;
9
+ if (schemaType.toSwagger) {
10
+ return schemaType.toSwagger();
9
11
  }
10
- // fallback
11
12
  return { type: 'string' };
12
13
  }
13
- function validateDataWithSchema(data, schema) {
14
- if (!data || typeof data !== 'object') {
15
- throw new Error('Invalid data: must be an object');
16
- }
17
- for (const [key, typeIs] of Object.entries(schema)) {
18
- const value = data[key];
19
- // TypeIs가 함수로 전달된 경우 실행
20
- const schemaType = typeof typeIs === 'function' ? typeIs() : typeIs;
21
- // optional 여부 확인
22
- const isOptional = schemaType.__options.optional || false;
23
- // 필수 필드 체크
24
- if (value === undefined && !isOptional) {
25
- throw new Error(`Missing required field: ${key}`);
26
- }
27
- // 값이 있을 때만 타입 체크
28
- if (value !== undefined) {
29
- validateFieldType(key, value, schemaType);
30
- }
31
- }
32
- }
33
- function validateFieldType(fieldName, value, schemaType) {
34
- // @asapjs/schema의 validate 메서드 사용
35
- if (!schemaType.validate(value)) {
36
- throw new Error(`Field '${fieldName}' has invalid type for ${schemaType.__name}`);
37
- }
38
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asapjs/error",
3
- "version": "1.0.0-alpha.0",
3
+ "version": "1.0.0-alpha.1",
4
4
  "description": "Error handling utilities for ASAP.js with Effect integration",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -13,7 +13,7 @@
13
13
  },
14
14
  "dependencies": {
15
15
  "@asapjs/common": "1.0.0-alpha.0",
16
- "@asapjs/schema": "1.0.0-alpha.0",
16
+ "@asapjs/schema": "1.0.0-alpha.1",
17
17
  "@asapjs/types": "1.0.0-alpha.0",
18
18
  "effect": "^3.11.11",
19
19
  "express": "^4.17.3"
@@ -28,5 +28,5 @@
28
28
  "publishConfig": {
29
29
  "access": "public"
30
30
  },
31
- "gitHead": "79561e3604dc505813035558d1bfcc6cda0b088a"
31
+ "gitHead": "84779254dfe56c6e01cfa219b6234058cae9aca8"
32
32
  }