Skip to content

Commit

Permalink
✨ feat: 添加错误捕捉组件
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Apr 10, 2021
1 parent 5399d08 commit ea2a857
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 15 deletions.
1 change: 1 addition & 0 deletions packages/journey-map/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"antd": "^4.12.3",
"classnames": "^2.2.6",
"js-yaml": "^4.0.0",
"react-error-boundary": "^3.1.1",
"use-merge-value": "^1.0.2",
"use-overflow": "^1.1.0"
},
Expand Down
8 changes: 8 additions & 0 deletions packages/journey-map/src/components/ErrorFallback.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@import '../token';

.avx-journey-error {
&-detail {
margin-top: 8px;
.secondText;
}
}
27 changes: 27 additions & 0 deletions packages/journey-map/src/components/ErrorFallback.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react';
import { Alert, Typography } from 'antd';

import './ErrorFallback.less';

const { Title, Text } = Typography;
const ErrorFallback = ({ error }) => (
<Alert
type={'error'}
message={
<div>
<Title level={5} className="avx-journey-error-title">
渲染出错啦 😮
</Title>
<Text className="avx-journey-error-desc">
请检查输入语法是否存在问题,错误详情如下:
</Text>
<pre>
<Text ellipsis copyable className="avx-journey-error-detail">
{error.message}
</Text>
</pre>
</div>
}
/>
);
export default ErrorFallback;
1 change: 1 addition & 0 deletions packages/journey-map/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as Section } from './Section';
export { default as OverflowTitle } from './OverflowTitle';
export { default as ErrorFallback } from './ErrorFallback';
34 changes: 19 additions & 15 deletions packages/journey-map/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React from 'react';
import type { FC, CSSProperties } from 'react';
import cls from 'classnames';
import { ErrorBoundary } from 'react-error-boundary';

import Stage from './Stages';
import Chart from './Chart';
import Actions from './Actions';
import Thoughts from './Thoughts';
import { ErrorFallback } from './components';

import { JourneyMapStore, useJourneyMap } from './useJourneyMap';
import type { Config, JourneyMapData, SectionType } from './types';
Expand Down Expand Up @@ -64,22 +66,24 @@ const JourneyMap: FC<JourneyMapProps> = ({
];

return (
<JourneyMapStore.Provider value={store}>
<div
className={cls('avx-journey-map-container', className)}
style={style}
>
{store.title ? (
<div className="avx-journey-map-title">{store.title}</div>
) : null}
<div className="avx-journey-map-content">
{arrange.map((a) => {
const Section = sections[a];
return <Section key={a} />;
})}
<ErrorBoundary FallbackComponent={ErrorFallback}>
<JourneyMapStore.Provider value={store}>
<div
className={cls('avx-journey-map-container', className)}
style={style}
>
{store.title ? (
<div className="avx-journey-map-title">{store.title}</div>
) : null}
<div className="avx-journey-map-content">
{arrange.map((a) => {
const Section = sections[a];
return <Section key={a} />;
})}
</div>
</div>
</div>
</JourneyMapStore.Provider>
</JourneyMapStore.Provider>
</ErrorBoundary>
);
};

Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16972,6 +16972,13 @@ react-error-boundary@^3.1.0:
dependencies:
"@babel/runtime" "^7.12.5"

react-error-boundary@^3.1.1:
version "3.1.1"
resolved "https://registry.npm.taobao.org/react-error-boundary/download/react-error-boundary-3.1.1.tgz#932c5ca5cbab8ec4fe37fd7b415aa5c3a47597e7"
integrity sha1-kyxcpcurjsT+N/17QVqlw6R1l+c=
dependencies:
"@babel/runtime" "^7.12.5"

react-fast-compare@^3.1.1:
version "3.2.0"
resolved "https://registry.npm.taobao.org/react-fast-compare/download/react-fast-compare-3.2.0.tgz#641a9da81b6a6320f270e89724fb45a0b39e43bb"
Expand Down

0 comments on commit ea2a857

Please sign in to comment.