@asapjs/error 1.0.0-alpha.33 → 1.0.0-alpha.37
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/customFactory.d.ts +26 -0
- package/dist/customFactory.js +70 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +5 -5
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { HttpError } from './types';
|
|
2
|
+
export declare function setCustomFactoryAddScheme(func: any): void;
|
|
3
|
+
export declare class CustomHttpError extends HttpError {
|
|
4
|
+
private _serialized;
|
|
5
|
+
constructor(status: number, errorCode: string, message: string, serialized: Record<string, any>);
|
|
6
|
+
toJSON(): any;
|
|
7
|
+
}
|
|
8
|
+
export interface CustomErrorCreator<T = any> {
|
|
9
|
+
(data: T): CustomHttpError;
|
|
10
|
+
_status: number;
|
|
11
|
+
_code: string;
|
|
12
|
+
_message: string;
|
|
13
|
+
_schema: Record<string, any>;
|
|
14
|
+
_swaggerSchema: Record<string, any>;
|
|
15
|
+
_isCustom: true;
|
|
16
|
+
}
|
|
17
|
+
export interface CreateErrorFactoryOptions {
|
|
18
|
+
dto: new () => any;
|
|
19
|
+
serialize: (params: {
|
|
20
|
+
status: number;
|
|
21
|
+
code: string;
|
|
22
|
+
message: string;
|
|
23
|
+
data: any;
|
|
24
|
+
}) => Record<string, any>;
|
|
25
|
+
}
|
|
26
|
+
export declare function createErrorFactory(options: CreateErrorFactoryOptions): <T extends Record<string, any>>(status: number, code: string, message: string, schema: T) => CustomErrorCreator<{ [K in keyof T]: any; }>;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CustomHttpError = void 0;
|
|
4
|
+
exports.setCustomFactoryAddScheme = setCustomFactoryAddScheme;
|
|
5
|
+
exports.createErrorFactory = createErrorFactory;
|
|
6
|
+
const types_1 = require("./types");
|
|
7
|
+
const schema_1 = require("@asapjs/schema");
|
|
8
|
+
const schema_2 = require("./schema");
|
|
9
|
+
let customAddSchemeFunc = () => { };
|
|
10
|
+
function setCustomFactoryAddScheme(func) {
|
|
11
|
+
customAddSchemeFunc = func;
|
|
12
|
+
}
|
|
13
|
+
class CustomHttpError extends types_1.HttpError {
|
|
14
|
+
constructor(status, errorCode, message, serialized) {
|
|
15
|
+
super(status, errorCode, message, serialized.data);
|
|
16
|
+
this._serialized = serialized;
|
|
17
|
+
}
|
|
18
|
+
toJSON() {
|
|
19
|
+
return this._serialized;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.CustomHttpError = CustomHttpError;
|
|
23
|
+
function createErrorFactory(options) {
|
|
24
|
+
const { dto, serialize } = options;
|
|
25
|
+
const instance = new dto();
|
|
26
|
+
const dtoSwaggerSchema = typeof instance.generateScheme === 'function'
|
|
27
|
+
? instance.generateScheme()
|
|
28
|
+
: { type: 'object', properties: {} };
|
|
29
|
+
function customError(status, code, message, schema) {
|
|
30
|
+
const registerSchema = () => {
|
|
31
|
+
const dataProperties = Object.entries(schema).reduce((acc, [key, typeIs]) => {
|
|
32
|
+
acc[key] = (0, schema_2.typeIsToSwaggerSchema)(typeIs);
|
|
33
|
+
return acc;
|
|
34
|
+
}, {});
|
|
35
|
+
const errorSwaggerSchema = {
|
|
36
|
+
...dtoSwaggerSchema,
|
|
37
|
+
properties: {
|
|
38
|
+
...dtoSwaggerSchema.properties,
|
|
39
|
+
data: { type: 'object', properties: dataProperties },
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
customAddSchemeFunc({ name: code, data: errorSwaggerSchema });
|
|
43
|
+
};
|
|
44
|
+
try {
|
|
45
|
+
registerSchema();
|
|
46
|
+
}
|
|
47
|
+
catch (_a) { }
|
|
48
|
+
const creator = ((data) => {
|
|
49
|
+
const mappedData = (0, schema_1.mapDataWithSchema)(data && typeof data === 'object' ? data : {}, schema);
|
|
50
|
+
const finalMessage = message.replace(/\{(\w+)\}/g, (match, key) => {
|
|
51
|
+
return mappedData[key] !== undefined ? String(mappedData[key]) : match;
|
|
52
|
+
});
|
|
53
|
+
const serialized = serialize({
|
|
54
|
+
status,
|
|
55
|
+
code,
|
|
56
|
+
message: finalMessage,
|
|
57
|
+
data: mappedData,
|
|
58
|
+
});
|
|
59
|
+
return new CustomHttpError(status, code, finalMessage, serialized);
|
|
60
|
+
});
|
|
61
|
+
creator._status = status;
|
|
62
|
+
creator._code = code;
|
|
63
|
+
creator._message = message;
|
|
64
|
+
creator._schema = schema;
|
|
65
|
+
creator._swaggerSchema = dtoSwaggerSchema;
|
|
66
|
+
creator._isCustom = true;
|
|
67
|
+
return creator;
|
|
68
|
+
}
|
|
69
|
+
return customError;
|
|
70
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -19,4 +19,5 @@ __exportStar(require("./helpers"), exports);
|
|
|
19
19
|
__exportStar(require("./middleware"), exports);
|
|
20
20
|
__exportStar(require("./factory"), exports);
|
|
21
21
|
__exportStar(require("./schema"), exports);
|
|
22
|
+
__exportStar(require("./customFactory"), exports);
|
|
22
23
|
// HttpException은 router 패키지에서 직접 import 해야 함 (순환 의존성 방지)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@asapjs/error",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.37",
|
|
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",
|
|
@@ -12,9 +12,9 @@
|
|
|
12
12
|
"dev": "tsc --watch"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@asapjs/common": "^1.0.0-alpha.
|
|
16
|
-
"@asapjs/schema": "^1.0.0-alpha.
|
|
17
|
-
"@asapjs/types": "^1.0.0-alpha.
|
|
15
|
+
"@asapjs/common": "^1.0.0-alpha.37",
|
|
16
|
+
"@asapjs/schema": "^1.0.0-alpha.37",
|
|
17
|
+
"@asapjs/types": "^1.0.0-alpha.37",
|
|
18
18
|
"effect": "^3.11.11",
|
|
19
19
|
"express": "^4.20.0"
|
|
20
20
|
},
|
|
@@ -29,5 +29,5 @@
|
|
|
29
29
|
"publishConfig": {
|
|
30
30
|
"access": "public"
|
|
31
31
|
},
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "549cb8d70521be419232501936648301da4d3d9d"
|
|
33
33
|
}
|