Skip to content

Commit 16dde35

Browse files
committed
Merge branch 'fix/write-invalid-tag-crash' into develop
2 parents b023c19 + 5542250 commit 16dde35

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

src/components/common/MarkdownRender.tsx

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ const MarkdownRender: React.FC<MarkdownRenderProps> = ({
173173
);
174174

175175
const [element, setElement] = useState<RenderedElement>(null);
176+
const [hasTagError, setHasTagError] = useState(false);
176177

177178
const applyElement = React.useMemo(() => {
178179
return throttle(250, (el: any) => {
@@ -205,8 +206,8 @@ const MarkdownRender: React.FC<MarkdownRenderProps> = ({
205206
}
206207

207208
try {
208-
const el = parse(filter(html));
209-
// const el = parse(html);
209+
const el = parse(html);
210+
setHasTagError(false);
210211
applyElement(el);
211212
} catch (e) {}
212213
});
@@ -215,9 +216,14 @@ const MarkdownRender: React.FC<MarkdownRenderProps> = ({
215216
return (
216217
<Typography>
217218
{editing ? (
218-
<MarkdownRenderBlock className={codeTheme}>
219-
{element}
220-
</MarkdownRenderBlock>
219+
<MarkdownRenderErrorBoundary
220+
onError={() => setHasTagError(true)}
221+
hasTagError={hasTagError}
222+
>
223+
<MarkdownRenderBlock className={codeTheme}>
224+
{element}
225+
</MarkdownRenderBlock>
226+
</MarkdownRenderErrorBoundary>
221227
) : (
222228
<MarkdownRenderBlock
223229
className={codeTheme}
@@ -228,4 +234,35 @@ const MarkdownRender: React.FC<MarkdownRenderProps> = ({
228234
);
229235
};
230236

237+
type ErrorBoundaryProps = {
238+
onError: () => void;
239+
hasTagError: boolean;
240+
};
241+
class MarkdownRenderErrorBoundary extends React.Component<ErrorBoundaryProps> {
242+
state = {
243+
hasError: false,
244+
};
245+
componentDidCatch() {
246+
this.setState({
247+
hasError: true,
248+
});
249+
this.props.onError();
250+
}
251+
252+
componentDidUpdate(prevProps: ErrorBoundaryProps) {
253+
if (prevProps.hasTagError && !this.props.hasTagError) {
254+
this.setState({
255+
hasError: false,
256+
});
257+
}
258+
}
259+
260+
render() {
261+
if (this.state.hasError) {
262+
return <div>HTML 태그 파싱 실패</div>;
263+
}
264+
return this.props.children;
265+
}
266+
}
267+
231268
export default React.memo(MarkdownRender);

src/components/error/CrashErrorScreen.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,21 @@ import ErrorScreenTemplate from './ErrorScreenTemplate';
44
import { undrawBugFixing } from '../../static/images';
55
import { useHistory } from 'react-router-dom';
66

7-
export type CrashErrorScreenProps = {};
7+
export type CrashErrorScreenProps = {
8+
onResolve: () => void;
9+
};
810

9-
function CrashErrorScreen(props: CrashErrorScreenProps) {
11+
function CrashErrorScreen({ onResolve }: CrashErrorScreenProps) {
1012
const history = useHistory();
1113
return (
1214
<ErrorScreenTemplate
1315
image={undrawBugFixing}
1416
message="엇! 오류가 발생했습니다."
1517
buttonText="홈으로"
16-
onButtonClick={() => history.push('/')}
18+
onButtonClick={() => {
19+
history.push('/');
20+
onResolve();
21+
}}
1722
/>
1823
);
1924
}

src/components/error/ErrorBoundary.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,18 @@ class ErrorBoundary extends React.Component {
2323
Sentry.captureException(error);
2424
}
2525
}
26+
27+
handleResolveError = () => {
28+
this.setState({
29+
hasError: false,
30+
});
31+
};
2632
render() {
2733
if (this.state.chunkError) {
2834
return <ChunkErrorScreen />;
2935
}
3036
if (this.state.hasError) {
31-
return <CrashErrorScreen />;
37+
return <CrashErrorScreen onResolve={this.handleResolveError} />;
3238
}
3339
return (
3440
<ErrorBoundaryWrapper hasError={this.state.hasError}>

0 commit comments

Comments
 (0)