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

Commit 52254cf

Browse files
committed
feat: error prompt2
1 parent 9d9656e commit 52254cf

File tree

13 files changed

+128
-124
lines changed

13 files changed

+128
-124
lines changed

packages/ui/xconsole-error-center/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@
22

33
> XConsole 错误处理库
44
5+
## Usage
6+
7+
```js
8+
import { ErrorPrompt as errorPrompt } from '@alicloud/xconsole-error-center';
9+
10+
errorPrompt({
11+
new Error('There is an error'),
12+
{
13+
errorConfig: {
14+
message,
15+
confirmLabel,
16+
cancelLabel,
17+
cancelHref,
18+
confirmHref
19+
},
20+
dialogType,
21+
disableExtraInfo,
22+
}
23+
});
24+
```
525

626
## Usage
727

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import isFunction from 'lodash/isFunction';
2+
import innerErrorPrompt from './ErrorPrompt';
3+
import { ResponseError, ErrorCenterOption } from './type';
4+
5+
6+
const createErrorCenter = (errorPrompt: typeof innerErrorPrompt) => (errorCenterOption: ErrorCenterOption) => {
7+
const {
8+
enable = false, errorCode, errorCodes,
9+
getMessage, disableExtraInfo, dialogType, showCopy
10+
} = errorCenterOption;
11+
12+
if (process.env.NODE_ENV === 'development' && errorCode) {
13+
console.error('[XConsole error-center]', '使用 errorCodes 来替代 errorCode(已废弃) ');
14+
}
15+
16+
return {
17+
onError(err: ResponseError) {
18+
if (!enable) return false
19+
20+
const valideErrorCodes = Object.assign(
21+
{},
22+
isFunction(errorCodes) ? errorCodes(err) : errorCodes || errorCode || {}
23+
);
24+
25+
if (process.env.NODE_ENV === 'development') {
26+
console.error('[XConsole error-center]', err, err.response);
27+
}
28+
29+
errorPrompt({
30+
error: err,
31+
errorConfig: valideErrorCodes[err.response?.data?.code] || errorCenterOption.errorConfig ||{},
32+
getMessage,
33+
showCopy,
34+
disableExtraInfo,
35+
dialogType
36+
})
37+
// @ts-ignore
38+
err.preventDefault && err.preventDefault()
39+
},
40+
}
41+
};
42+
43+
export default createErrorCenter;

packages/ui/xconsole-error-center/src/ErrorPrompt.tsx renamed to packages/ui/xconsole-error-center/src/errorPrompt/ErrorPrompt.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import errorPrompt from '@alicloud/xconsole-rc-error-prompt';
66
import pick from 'lodash/pick'
77
import {
88
ConsoleNeedLogin, PostonlyOrTokenError
9-
} from './constants'
10-
import { LOCALE, intl } from './utils';
9+
} from '../constants'
10+
import { LOCALE, intl } from '../utils';
1111
import {
1212
ResponseError, ErrorCodeConfig,
1313
GetMessageCallback, ErrorCodeConfigCallback
14-
} from './type';
14+
} from '../type';
1515

1616
interface ShowErrorOption {
1717
error: ResponseError;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import createErrorCenter from '../createErrorCenter';
2+
import { ResponseError, ErrorPromptOption } from '../type';
3+
import errorPrompt from './ErrorPrompt';
4+
5+
6+
export default createErrorCenter(errorPrompt)
7+
export const ErrorPrompt = (error: ResponseError, option: ErrorPromptOption = {}) => errorPrompt({ error, ...option })

packages/ui/xconsole-error-center/src/ErrorPrompt2.tsx renamed to packages/ui/xconsole-error-center/src/errorPrompt2/ErrorPrompt2.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import errorPrompt from '@alicloud/console-base-error-prompt-proxy';
44
import {
55
ResponseError, ErrorCodeConfig,
66
GetMessageCallback, ErrorCodeConfigCallback
7-
} from './type';
7+
} from '../type';
88

99
interface ShowErrorOption {
1010
error: ResponseError;
@@ -13,7 +13,6 @@ interface ShowErrorOption {
1313
}
1414

1515
const getRealMessage = (error: ResponseError, errorConfig: Partial<ErrorCodeConfig>, getMessage?: GetMessageCallback) => {
16-
1716
if (errorConfig.message) {
1817
if (isFunction(errorConfig.message)) {
1918
return errorConfig.message(error);
@@ -63,7 +62,7 @@ const processError = (errorConfig: Partial<ErrorCodeConfig>, error: ResponseErro
6362
}
6463
}
6564

66-
const showError = (option: ShowErrorOption) => {
65+
const errorPrompt2 = (option: ShowErrorOption) => {
6766
const { error, getMessage } = option;
6867
const code = error?.response?.data?.code;
6968
const errorConfig = getErrorConfig(option.errorConfig, error, getMessage);
@@ -76,7 +75,7 @@ const showError = (option: ShowErrorOption) => {
7675
return;
7776
}
7877

79-
errorPrompt(processError(errorConfig, error), {
78+
return errorPrompt(processError(errorConfig, error), {
8079
title: errorConfig.title,
8180
button: errorConfig.confirmLabel && {
8281
href: errorConfig.confirmHref,
@@ -85,4 +84,4 @@ const showError = (option: ShowErrorOption) => {
8584
})
8685
}
8786

88-
export default showError;
87+
export default errorPrompt2;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import createErrorCenter from '../createErrorCenter';
2+
import { ResponseError, ErrorPromptOption } from '../type';
3+
import errorPrompt from './ErrorPrompt2';
4+
5+
6+
export default createErrorCenter(errorPrompt)
7+
export const ErrorPrompt = (error: ResponseError, option: ErrorPromptOption = {}) => errorPrompt({ error, ...option })
Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,9 @@
1-
import isFunction from 'lodash/isFunction';
2-
import innerErrorPrompt from './ErrorPrompt';
3-
import { ResponseError, ErrorCenterOption, ErrorPromptOption } from './type';
1+
import ErrorCenter from './errorPrompt/index'
42

5-
const ErrorCenter = (errorCenterOption: ErrorCenterOption) => {
6-
const {
7-
enable = false, errorCode, errorCodes,
8-
getMessage, disableExtraInfo, dialogType, showCopy
9-
} = errorCenterOption;
10-
11-
if (process.env.NODE_ENV === 'development' && errorCode) {
12-
console.error('[XConsole error-center]', '使用 errorCodes 来替代 errorCode(已废弃) ');
13-
}
14-
15-
return {
16-
onError(err: ResponseError) {
17-
if (!enable) return false
18-
19-
const valideErrorCodes = Object.assign(
20-
{},
21-
isFunction(errorCodes) ? errorCodes(err) : errorCodes || errorCode || {}
22-
);
23-
24-
if (process.env.NODE_ENV === 'development') {
25-
console.error('[XConsole error-center]', err, err.response);
26-
}
27-
28-
innerErrorPrompt({
29-
error: err,
30-
errorConfig: valideErrorCodes[err.response?.data?.code] || errorCenterOption.errorConfig ||{},
31-
getMessage,
32-
showCopy,
33-
disableExtraInfo,
34-
dialogType
35-
})
36-
// @ts-ignore
37-
err.preventDefault && err.preventDefault()
38-
},
39-
}
40-
};
41-
42-
export const ErrorPrompt = (
43-
error: ResponseError,
44-
option: ErrorPromptOption = {}
45-
) => innerErrorPrompt({ error, ...option })
46-
47-
export * from './type';
3+
// 优化过的弹窗,不过不兼容之前API 所以单独独立出来
4+
export { ErrorPrompt as ErrorPrompt2 } from './errorPrompt2';
5+
export { default as ErrorCenter2 } from './errorPrompt2';
486

7+
// legacy api
8+
export { ErrorPrompt } from './errorPrompt';
499
export default ErrorCenter;

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

Lines changed: 0 additions & 36 deletions
This file was deleted.

packages/ui/xconsole-error-center/stories/demo/basic.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import ErrorPrompt from '../../src/ErrorPrompt';
2+
import ErrorPrompt from '../../src/errorPrompt';
33

44
const promptError = () => ErrorPrompt({ showCopy: true, error: new Error('There is an error') });
55

packages/ui/xconsole-error-center/stories/demo/request.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22
import axios from 'axios';
3-
import ErrorPrompt from '../../src/ErrorPrompt';
3+
import ErrorPrompt from '../../src/errorPrompt';
44

55
const promptError = async () => {
66
try {
@@ -16,7 +16,7 @@ const promptError = async () => {
1616
cancelLabel: "留在页面", // 取消按钮文案
1717
cancelHref: "https://aliyun.com" // 点击取消跳转的链接
1818
}
19-
})
19+
})
2020
}
2121
};
2222

0 commit comments

Comments
 (0)