Skip to content

Commit 50ff34f

Browse files
authored
Merge pull request velopert#125 from velopert/fix/write-invalid-tag-crash
Fix/write invalid tag crash
2 parents a545beb + 5542250 commit 50ff34f

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

src/components/common/MarkdownRender.tsx

Lines changed: 45 additions & 6 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) => {
@@ -204,18 +205,25 @@ const MarkdownRender: React.FC<MarkdownRenderProps> = ({
204205
return;
205206
}
206207

207-
const el = parse(editing ? html : filter(html));
208-
209-
applyElement(el);
208+
try {
209+
const el = parse(html);
210+
setHasTagError(false);
211+
applyElement(el);
212+
} catch (e) {}
210213
});
211214
}, [applyElement, editing, markdown, onConvertFinish]);
212215

213216
return (
214217
<Typography>
215218
{editing ? (
216-
<MarkdownRenderBlock className={codeTheme}>
217-
{element}
218-
</MarkdownRenderBlock>
219+
<MarkdownRenderErrorBoundary
220+
onError={() => setHasTagError(true)}
221+
hasTagError={hasTagError}
222+
>
223+
<MarkdownRenderBlock className={codeTheme}>
224+
{element}
225+
</MarkdownRenderBlock>
226+
</MarkdownRenderErrorBoundary>
219227
) : (
220228
<MarkdownRenderBlock
221229
className={codeTheme}
@@ -226,4 +234,35 @@ const MarkdownRender: React.FC<MarkdownRenderProps> = ({
226234
);
227235
};
228236

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+
229268
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)