Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Commit b66f700

Browse files
committed
feat: parse error for multiapi
1 parent bad9e57 commit b66f700

File tree

3 files changed

+71
-30
lines changed

3 files changed

+71
-30
lines changed

packages/console-utils/xconsole-service/src/interceptors/armsInterceptor/response.ts

Lines changed: 65 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,53 @@ import URLSearchParams from '@ungap/url-search-params';
22
import { IResponse, IResponseData } from '../../types';
33
import logger from '../../utils/logger';
44

5+
const logArmsForMultiApi = ({targetUrl, traceId, startTime, duration, success, code, msg}, response: IResponse<IResponseData>) => {
6+
let api = targetUrl;
7+
const { config, data } = response;
8+
const { data: dataStr } = config;
9+
const requestData = new URLSearchParams(dataStr);
10+
try {
11+
const actions: { action: string }[] = JSON.parse(
12+
requestData.get('actions') || '[]'
13+
);
14+
let apiIdentifier = '';
15+
const actionSet: any = {};
16+
for (const item of actions) {
17+
if (!actionSet[item.action]) {
18+
actionSet[item.action] = 1;
19+
}
20+
}
21+
apiIdentifier = Object.keys(actionSet).join(',');
22+
api = `${targetUrl}?product=${requestData.get(
23+
'product'
24+
)}&action=${apiIdentifier}`;
25+
} catch (err) {
26+
api = targetUrl;
27+
}
28+
29+
// 如果是 mutiapi 一起失败了, 直接上报
30+
if (!success) {
31+
logger({ api, success, duration, code, msg, startTime, traceId });
32+
} else {
33+
const responseData = (data?.data || {});
34+
Object.keys(responseData).forEach((key) => {
35+
const respForSingleApi = responseData[key];
36+
if (respForSingleApi.Code && respForSingleApi.Code !== '200') {
37+
const apiWithIdentifier = `${api}&identifier=${key}&requestId=${respForSingleApi.RequestId}`;
38+
console.log({
39+
api: apiWithIdentifier, success: false, duration, code: respForSingleApi.Code,
40+
msg: respForSingleApi?.Message, startTime, traceId
41+
})
42+
logger({
43+
api: apiWithIdentifier, success: false, duration, code: respForSingleApi.Code,
44+
msg: respForSingleApi?.Message, startTime, traceId
45+
});
46+
}
47+
})
48+
}
49+
}
50+
51+
552
function armsResponseInterceptor(
653
response: IResponse<IResponseData>
754
): IResponse<IResponseData> {
@@ -40,40 +87,30 @@ function armsResponseInterceptor(
4087
const [targetUrl, targetApiType] = matches;
4188
let api = targetUrl;
4289
if (targetApiType.includes('multi')) {
43-
try {
44-
const actions: { action: string }[] = JSON.parse(
45-
requestData.get('actions') || '[]'
46-
);
47-
let apiIdentifier = '';
48-
const actionSet: any = {};
49-
for (const item of actions) {
50-
if (!actionSet[item.action]) {
51-
actionSet[item.action] = 1;
52-
}
53-
}
54-
apiIdentifier = Object.keys(actionSet).join(',');
55-
api = `${targetUrl}?product=${requestData.get(
56-
'product'
57-
)}&action=${apiIdentifier}`;
58-
} catch (err) {
59-
api = targetUrl;
60-
}
90+
logArmsForMultiApi({
91+
targetUrl,
92+
success: isSuccess,
93+
duration,
94+
code: apiCode,
95+
msg,
96+
startTime,
97+
traceId,
98+
}, response)
6199
} else {
62100
api = `${targetUrl}?product=${requestData.get(
63101
'product'
64102
)}&action=${requestData.get('action')}`;
103+
logger({
104+
api,
105+
success: isSuccess,
106+
duration,
107+
code: apiCode,
108+
msg,
109+
startTime,
110+
traceId,
111+
});
65112
}
66113

67-
logger({
68-
api,
69-
success: isSuccess,
70-
duration,
71-
code: apiCode,
72-
msg,
73-
startTime,
74-
traceId,
75-
});
76-
77114
return response;
78115
}
79116

packages/console-utils/xconsole-service/stories/index.stories.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { select, withKnobs } from '@storybook/addon-knobs';
33
// import withAxiosDecorator from 'storybook-axios';
44
import { storiesOf } from '@storybook/react';
55
import { createService, useOpenApi, useRoaApi, defaultAxiosRequest } from '../src/index'
6-
import { getOssDownloadUrl, genOssUploadSignature } from '../src/oss/index'
6+
import { getOssDownloadUrl } from '../src/oss/index'
77
import { ApiType } from '../src/const';
88
import '@alicloud/console-components/dist/wind.css'
99

@@ -12,6 +12,10 @@ defaultAxiosRequest.interceptors.request.handlers.unshift({
1212
fulfilled: (config) => {
1313
config.baseURL = 'https://oneapi.alibaba-inc.com/mock/oneconsole';
1414
config.method = 'GET'
15+
if(config.url.indexOf('multiApi.json')) {
16+
config.url = config.url.replace(/multiApi.json/, 'api.json')
17+
config.url = config.url.replace(/action=undefined/, 'action=DescribeMultiApi')
18+
}
1519
return config;
1620
}
1721
});

packages/console-utils/xconsole-service/stories/multiApi.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ storiesOf('XConsole Mulit Api', module)
1515
.add('createService', () => {
1616
useEffect(() => {
1717
try {
18-
createService('slb')(actions)
18+
createService('consoledemo')(actions)
1919
} catch(e) {
2020
console.log(e)
2121
}

0 commit comments

Comments
 (0)