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

Commit 9d9656e

Browse files
committed
feat: error prompt2 for new design
1 parent 12d0cdf commit 9d9656e

File tree

8 files changed

+228
-4
lines changed

8 files changed

+228
-4
lines changed

packages/ui/xconsole-error-center/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"react-dom": "^16.8.6"
4141
},
4242
"dependencies": {
43+
"@alicloud/console-base-error-prompt-proxy": "^1.3.5",
4344
"lodash": "^4.0.0",
4445
"react-copy-to-clipboard": "^5.0.3"
4546
},

packages/ui/xconsole-error-center/src/ErrorPrompt.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ const getErrorConfig = (
5757
};
5858
}
5959

60-
// TODO: Error 按钮的隐藏
61-
// TODO: Icon 的展示
62-
// TODO: 新开窗口
63-
6460
const CopyIcon = ({data}: {data: string}) => {
6561
return (
6662
<Balloon align="r" trigger={
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import isFunction from 'lodash/isFunction';
2+
import errorPrompt from '@alicloud/console-base-error-prompt-proxy';
3+
4+
import {
5+
ResponseError, ErrorCodeConfig,
6+
GetMessageCallback, ErrorCodeConfigCallback
7+
} from './type';
8+
9+
interface ShowErrorOption {
10+
error: ResponseError;
11+
errorConfig?: Partial<ErrorCodeConfig> | ErrorCodeConfigCallback;
12+
getMessage?: GetMessageCallback;
13+
}
14+
15+
const getRealMessage = (error: ResponseError, errorConfig: Partial<ErrorCodeConfig>, getMessage?: GetMessageCallback) => {
16+
17+
if (errorConfig.message) {
18+
if (isFunction(errorConfig.message)) {
19+
return errorConfig.message(error);
20+
}
21+
return errorConfig.message;
22+
}
23+
24+
const code = error?.response?.data?.code || 'UNKNOWN_ERROR';
25+
const errorMessage = error?.response?.data?.message || error.message || error?.response?.data?.code;
26+
if (getMessage) {
27+
return getMessage(code, errorMessage, error)
28+
}
29+
30+
return errorMessage;
31+
}
32+
33+
const getErrorConfig = (
34+
errorConfig: Partial<ErrorCodeConfig> | ErrorCodeConfigCallback | undefined,
35+
error: ResponseError,
36+
getMessage?: GetMessageCallback
37+
) => {
38+
39+
if (isFunction(errorConfig)) {
40+
return errorConfig(error)
41+
}
42+
43+
return {
44+
...errorConfig,
45+
message: getRealMessage(error, errorConfig || {}, getMessage)
46+
};
47+
}
48+
49+
50+
const processError = (errorConfig: Partial<ErrorCodeConfig>, error: ResponseError) => {
51+
return {
52+
name: error.name,
53+
message: errorConfig.message,
54+
title: errorConfig.title,
55+
requestId: error?.response?.data.requestId,
56+
details: {
57+
url: error?.response?.config.url,
58+
method: error?.response?.config?.method,
59+
params: error?.response?.config?.params,
60+
},
61+
code: error?.response?.data?.code,
62+
stack: error.stack,
63+
}
64+
}
65+
66+
const showError = (option: ShowErrorOption) => {
67+
const { error, getMessage } = option;
68+
const code = error?.response?.data?.code;
69+
const errorConfig = getErrorConfig(option.errorConfig, error, getMessage);
70+
71+
if(process.env.NODE_ENV === 'development' && (errorConfig.cancelLabel || errorConfig.cancelHref)) {
72+
console.warn(`[XConsole error-center]: cancelLabel and cancelHref will be ignored`)
73+
}
74+
75+
if (errorConfig.ignore || code === 'FoundRiskAndDoubleConfirm') {
76+
return;
77+
}
78+
79+
errorPrompt(processError(errorConfig, error), {
80+
title: errorConfig.title,
81+
button: errorConfig.confirmLabel && {
82+
href: errorConfig.confirmHref,
83+
children: errorConfig.confirmLabel,
84+
}
85+
})
86+
}
87+
88+
export default showError;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import isFunction from 'lodash/isFunction';
2+
import innerErrorPrompt from './ErrorPrompt2';
3+
import { ResponseError, ErrorCenterOption, ErrorPromptOption } from './type';
4+
5+
const ErrorCenter = (errorCenterOption: ErrorCenterOption) => {
6+
const { enable = false, errorCodes, getMessage } = errorCenterOption;
7+
8+
return {
9+
onError(err: ResponseError) {
10+
if (!enable) return false
11+
12+
const valideErrorCodes = Object.assign({}, isFunction(errorCodes) ? errorCodes(err) : errorCodes || {});
13+
14+
if (process.env.NODE_ENV === 'development') {
15+
console.error('[XConsole error-center]', err, err.response);
16+
}
17+
18+
innerErrorPrompt({
19+
error: err,
20+
errorConfig: valideErrorCodes[err.response?.data?.code] || errorCenterOption.errorConfig ||{},
21+
getMessage,
22+
})
23+
// @ts-ignore
24+
err.preventDefault && err.preventDefault()
25+
},
26+
}
27+
};
28+
29+
export const ErrorPrompt = (
30+
error: ResponseError,
31+
option: ErrorPromptOption = {}
32+
) => innerErrorPrompt({ error, ...option })
33+
34+
export * from './type';
35+
36+
export default ErrorCenter;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as React from 'react';
2+
import ErrorPrompt from '../../src/ErrorPrompt2';
3+
4+
const promptError = () => ErrorPrompt({ error: new Error('There is an error') });
5+
6+
export default () => {
7+
return (
8+
<div>
9+
<button onClick={promptError}> show prompt </button>
10+
</div>
11+
)
12+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import * as React from 'react';
2+
import { select, text, boolean } from '@storybook/addon-knobs'
3+
import errorPrompt from '../../src/ErrorPrompt2';
4+
5+
export default () => {
6+
const message = text('Message', '自定义 Error ❌ ');
7+
const ignore = boolean('Ignore', false);
8+
const confirmLabel = text('ConfirmLabel', '去登录');
9+
const cancelLabel = text('CancelLabel', '知道了');
10+
const confirmHref = text('ConfirmHref', 'https://signin.aliyun.com/login.htm');
11+
const cancelHref = text('CancelHref', 'https://signin.aliyun.com/login.htm');
12+
13+
const promptError = () =>
14+
errorPrompt(
15+
{
16+
errorConfig: {
17+
title: '自定义 Error ❌',
18+
message,
19+
confirmLabel,
20+
cancelLabel,
21+
cancelHref,
22+
confirmHref,
23+
ignore,
24+
},
25+
error: new Error('There is an error')
26+
},
27+
);
28+
29+
return (
30+
<div>
31+
<button onClick={promptError}> show prompt </button>
32+
</div>
33+
)
34+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import * as React from 'react';
2+
import {boolean } from '@storybook/addon-knobs'
3+
import errorPrompt from '../../src/ErrorPrompt2';
4+
5+
const responseData = {
6+
config: {
7+
url: '/api/data.json',
8+
method: 'get',
9+
},
10+
data: {
11+
code: 'No_Permission1',
12+
message: '没有权限',
13+
requestId: 'xxxx-xxxx',
14+
data: {}
15+
}
16+
};
17+
18+
const getError = () => {
19+
const err = new Error('test');
20+
// @ts-ignore
21+
err.response = responseData;
22+
return err;
23+
}
24+
25+
const promptError = () => errorPrompt({
26+
error: getError(),
27+
errorConfig: {
28+
message: '没有权限',
29+
},
30+
getMessage(code) {
31+
return code;
32+
}
33+
});
34+
35+
export default () => {
36+
return (
37+
<div>
38+
<button onClick={promptError}> show prompt </button>
39+
</div>
40+
)
41+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as React from 'react';
2+
import { withKnobs } from '@storybook/addon-knobs'
3+
import { storiesOf } from '@storybook/react';
4+
import '@alicloud/xconsole-rc-error-prompt/dist/index.css';
5+
import '@alicloud/xconsole-rc-dialog/dist/index.css';
6+
import '@alicloud/console-components/dist/wind.css';
7+
import DemoBasic from './demo2/basic'
8+
import DemoConfig from './demo2/config'
9+
import DemoRequest from './demo2/request'
10+
11+
12+
storiesOf('XconsoleErrorPrompt2', module)
13+
.addDecorator(withKnobs)
14+
.add('Basic', () => <DemoBasic />)
15+
.add('Config', () => <DemoConfig />)
16+
.add('Request Error', () => <DemoRequest />)

0 commit comments

Comments
 (0)