/
framework.ts
234 lines (212 loc) 路 7.01 KB
/
framework.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { MidwayError, registerErrorCode } from './base';
import { ObjectIdentifier } from '../interface';
export const FrameworkErrorEnum = registerErrorCode('midway', {
UNKNOWN: 10000,
COMMON: 10001,
PARAM_TYPE: 10002,
DEFINITION_NOT_FOUND: 10003,
FEATURE_NO_LONGER_SUPPORTED: 10004,
FEATURE_NOT_IMPLEMENTED: 10004,
MISSING_CONFIG: 10006,
MISSING_RESOLVER: 10007,
DUPLICATE_ROUTER: 10008,
USE_WRONG_METHOD: 10009,
SINGLETON_INJECT_REQUEST: 10010,
MISSING_IMPORTS: 10011,
UTIL_HTTP_TIMEOUT: 10012,
INCONSISTENT_VERSION: 10013,
INVALID_CONFIG: 10014,
DUPLICATE_CLASS_NAME: 10015,
DUPLICATE_CONTROLLER_PREFIX_OPTIONS: 10016,
RETRY_OVER_MAX_TIME: 10017,
INVOKE_METHOD_FORBIDDEN: 10018,
CODE_INVOKE_TIMEOUT: 10019,
MAIN_FRAMEWORK_MISSING: 10020,
INVALID_CONFIG_PROPERTY: 10021,
} as const);
export class MidwayCommonError extends MidwayError {
constructor(message: string) {
super(message, FrameworkErrorEnum.COMMON);
}
}
export class MidwayParameterError extends MidwayError {
constructor(message?: string) {
super(message ?? 'Parameter type not match', FrameworkErrorEnum.PARAM_TYPE);
}
}
export class MidwayDefinitionNotFoundError extends MidwayError {
static readonly type = Symbol.for('#NotFoundError');
static isClosePrototypeOf(ins: MidwayDefinitionNotFoundError): boolean {
return ins
? ins[MidwayDefinitionNotFoundError.type] ===
MidwayDefinitionNotFoundError.type
: false;
}
constructor(identifier: ObjectIdentifier) {
super(
`${identifier} is not valid in current context`,
FrameworkErrorEnum.DEFINITION_NOT_FOUND
);
this[MidwayDefinitionNotFoundError.type] =
MidwayDefinitionNotFoundError.type;
}
updateErrorMsg(className: string): void {
const identifier = this.message.split(
' is not valid in current context'
)[0];
this.message = `${identifier} in class ${className} is not valid in current context`;
}
}
export class MidwayFeatureNoLongerSupportedError extends MidwayError {
constructor(message?: string) {
super(
'This feature no longer supported \n' + message,
FrameworkErrorEnum.FEATURE_NO_LONGER_SUPPORTED
);
}
}
export class MidwayFeatureNotImplementedError extends MidwayError {
constructor(message?: string) {
super(
'This feature not implemented \n' + message,
FrameworkErrorEnum.FEATURE_NOT_IMPLEMENTED
);
}
}
export class MidwayConfigMissingError extends MidwayError {
constructor(configKey: string) {
super(
`Can't found config key "${configKey}" in your config, please set it first`,
FrameworkErrorEnum.MISSING_CONFIG
);
}
}
export class MidwayInvalidConfigError extends MidwayError {
constructor(message?: string) {
super(
'Invalid config file \n' + message,
FrameworkErrorEnum.INVALID_CONFIG
);
}
}
export class MidwayResolverMissingError extends MidwayError {
constructor(type: string) {
super(
`Resolver "${type}" is missing.`,
FrameworkErrorEnum.MISSING_RESOLVER
);
}
}
export class MidwayDuplicateRouteError extends MidwayError {
constructor(routerUrl: string, existPos: string, existPosOther: string) {
super(
`Duplicate router "${routerUrl}" at "${existPos}" and "${existPosOther}"`,
FrameworkErrorEnum.DUPLICATE_ROUTER
);
}
}
export class MidwayUseWrongMethodError extends MidwayError {
constructor(
wrongMethod: string,
replacedMethod: string,
describeKey?: string
) {
const text = describeKey
? `${describeKey} not valid by ${wrongMethod}, Use ${replacedMethod} instead!`
: `You should not invoked by ${wrongMethod}, Use ${replacedMethod} instead!`;
super(text, FrameworkErrorEnum.USE_WRONG_METHOD);
}
}
export class MidwaySingletonInjectRequestError extends MidwayError {
constructor(singletonScopeName: string, requestScopeName: string) {
const text = `${singletonScopeName} with singleton scope can't implicitly inject ${requestScopeName} with request scope directly, please add "@Scope(ScopeEnum.Request, { allowDowngrade: true })" in ${requestScopeName} or use "ctx.requestContext.getAsync(${requestScopeName})".`;
super(text, FrameworkErrorEnum.SINGLETON_INJECT_REQUEST);
}
}
export class MidwayMissingImportComponentError extends MidwayError {
constructor(originName: string) {
const text = `"${originName}" can't inject and maybe forgot add "{imports: [***]}" in @Configuration.`;
super(text, FrameworkErrorEnum.MISSING_IMPORTS);
}
}
export class MidwayUtilHttpClientTimeoutError extends MidwayError {
constructor(message: string) {
super(message, FrameworkErrorEnum.UTIL_HTTP_TIMEOUT);
}
}
export class MidwayInconsistentVersionError extends MidwayError {
constructor() {
const text =
'We find a latest dependency package installed, please remove the lock file and use "npm update" to upgrade all dependencies first.';
super(text, FrameworkErrorEnum.INCONSISTENT_VERSION);
}
}
export class MidwayDuplicateClassNameError extends MidwayError {
constructor(className: string, existPath: string, existPathOther: string) {
super(
`"${className}" duplicated between "${existPath}" and "${existPathOther}"`,
FrameworkErrorEnum.DUPLICATE_CLASS_NAME
);
}
}
export class MidwayDuplicateControllerOptionsError extends MidwayError {
constructor(
prefix: string,
existController: string,
existControllerOther: string
) {
super(
`"Prefix ${prefix}" with duplicated controller options between "${existController}" and "${existControllerOther}"`,
FrameworkErrorEnum.DUPLICATE_CONTROLLER_PREFIX_OPTIONS
);
}
}
export class MidwayRetryExceededMaxTimesError extends MidwayError {
constructor(methodName, times: number, err: Error) {
super(
`Invoke "${methodName}" retries exceeded the maximum number of times(${times}), error: ${err.message}`,
FrameworkErrorEnum.RETRY_OVER_MAX_TIME,
{
cause: err,
}
);
}
}
export class MidwayInvokeForbiddenError extends MidwayError {
constructor(methodName: string, module?: any) {
super(
`Invoke "${
module ? module.name : 'unknown'
}.${methodName}" is forbidden.`,
FrameworkErrorEnum.INVOKE_METHOD_FORBIDDEN
);
}
}
export class MidwayCodeInvokeTimeoutError extends MidwayError {
constructor(methodName: string, timeout: number) {
super(
`Invoke "${methodName}" running timeout(${timeout}ms)`,
FrameworkErrorEnum.CODE_INVOKE_TIMEOUT
);
}
}
export class MidwayMainFrameworkMissingError extends MidwayError {
constructor() {
super(
'Main framework missing, please check your configuration.',
FrameworkErrorEnum.MAIN_FRAMEWORK_MISSING
);
}
}
export class MidwayInvalidConfigPropertyError extends MidwayError {
constructor(propertyName: string, allowTypes?: string[]) {
super(
`Invalid config property "${propertyName}", ${
allowTypes
? `only ${allowTypes.join(',')} can be set`
: 'please check your configuration'
}.`,
FrameworkErrorEnum.INVALID_CONFIG_PROPERTY
);
}
}