@cicctencent/midwayjs-base 1.0.47 → 1.0.48
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.
|
@@ -9,7 +9,8 @@ export declare enum ErrorCode {
|
|
|
9
9
|
IP_NOT_ALLOWED = 50004,
|
|
10
10
|
TIMESTAMP_EXPIRED = 50005,
|
|
11
11
|
TIMESTAMP_INVALID = 50006,
|
|
12
|
-
TOKEN_EMPTY = 50007
|
|
12
|
+
TOKEN_EMPTY = 50007,
|
|
13
|
+
OAUTH_ERROR = 50008
|
|
13
14
|
}
|
|
14
15
|
export declare const ErrorMessages: Record<ErrorCode, string>;
|
|
15
16
|
export declare function createError(code: ErrorCode, msg?: string): {
|
|
@@ -15,6 +15,7 @@ var ErrorCode;
|
|
|
15
15
|
ErrorCode[ErrorCode["TIMESTAMP_EXPIRED"] = 50005] = "TIMESTAMP_EXPIRED";
|
|
16
16
|
ErrorCode[ErrorCode["TIMESTAMP_INVALID"] = 50006] = "TIMESTAMP_INVALID";
|
|
17
17
|
ErrorCode[ErrorCode["TOKEN_EMPTY"] = 50007] = "TOKEN_EMPTY";
|
|
18
|
+
ErrorCode[ErrorCode["OAUTH_ERROR"] = 50008] = "OAUTH_ERROR";
|
|
18
19
|
})(ErrorCode || (exports.ErrorCode = ErrorCode = {}));
|
|
19
20
|
exports.ErrorMessages = {
|
|
20
21
|
[ErrorCode.SUCCESS]: 'success',
|
|
@@ -28,6 +29,7 @@ exports.ErrorMessages = {
|
|
|
28
29
|
[ErrorCode.TIMESTAMP_EXPIRED]: 'timestamp已超过有效时间',
|
|
29
30
|
[ErrorCode.TIMESTAMP_INVALID]: 'timestamp时间异常',
|
|
30
31
|
[ErrorCode.TOKEN_EMPTY]: 'token为空,非法请求',
|
|
32
|
+
[ErrorCode.OAUTH_ERROR]: 'OAuth认证失败',
|
|
31
33
|
};
|
|
32
34
|
function createError(code, msg) {
|
|
33
35
|
return {
|
|
@@ -4,5 +4,6 @@ export declare class ApiResultFormatterMiddleware implements IMiddleware<IMidway
|
|
|
4
4
|
ignore(ctx: IMidwayContext): boolean;
|
|
5
5
|
resolve(): (ctx: IMidwayContext, next: any) => Promise<void>;
|
|
6
6
|
isJsonRpc(data: any): boolean;
|
|
7
|
+
isOAuthError(data: any): boolean;
|
|
7
8
|
canFormat(data: any): boolean;
|
|
8
9
|
}
|
|
@@ -37,6 +37,15 @@ let ApiResultFormatterMiddleware = class ApiResultFormatterMiddleware {
|
|
|
37
37
|
if (this.isJsonRpc(res)) {
|
|
38
38
|
return;
|
|
39
39
|
}
|
|
40
|
+
if (this.isOAuthError(res)) {
|
|
41
|
+
koaCtx.body = {
|
|
42
|
+
ret: error_code_1.ErrorCode.OAUTH_ERROR,
|
|
43
|
+
msg: res.error_description || res.error || 'OAuth 认证失败',
|
|
44
|
+
error: res.error,
|
|
45
|
+
error_description: res.error_description,
|
|
46
|
+
};
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
40
49
|
if (this.canFormat(res)) {
|
|
41
50
|
koaCtx.body = Object.assign({
|
|
42
51
|
ret: 0,
|
|
@@ -52,6 +61,14 @@ let ApiResultFormatterMiddleware = class ApiResultFormatterMiddleware {
|
|
|
52
61
|
else if (err instanceof error_code_1.BizError || typeof err?.ret === 'number') {
|
|
53
62
|
koaCtx.body = err instanceof error_code_1.BizError ? err.toJSON() : err;
|
|
54
63
|
}
|
|
64
|
+
else if (this.isOAuthError(err)) {
|
|
65
|
+
koaCtx.body = {
|
|
66
|
+
ret: error_code_1.ErrorCode.OAUTH_ERROR,
|
|
67
|
+
msg: err.error_description || err.error || 'OAuth 认证失败',
|
|
68
|
+
error: err.error,
|
|
69
|
+
error_description: err.error_description,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
55
72
|
else {
|
|
56
73
|
koaCtx.body = {
|
|
57
74
|
ret: error_code_1.ErrorCode.SYSTEM_ERROR,
|
|
@@ -76,6 +93,14 @@ let ApiResultFormatterMiddleware = class ApiResultFormatterMiddleware {
|
|
|
76
93
|
'jsonrpc' in data &&
|
|
77
94
|
('result' in data || 'error' in data));
|
|
78
95
|
}
|
|
96
|
+
isOAuthError(data) {
|
|
97
|
+
return (data != null &&
|
|
98
|
+
typeof data === 'object' &&
|
|
99
|
+
'error' in data &&
|
|
100
|
+
typeof data.error === 'string' &&
|
|
101
|
+
!('jsonrpc' in data) &&
|
|
102
|
+
!('ret' in data));
|
|
103
|
+
}
|
|
79
104
|
canFormat(data) {
|
|
80
105
|
return (data !== '' &&
|
|
81
106
|
!Buffer.isBuffer(data) &&
|