Skip to content

Commit cc9ef6d

Browse files
committed
Add error boundary to ProgressiveCodeBlock
1 parent 40599a0 commit cc9ef6d

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

src/components/ErrorCatcher.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React, { ErrorInfo } from 'react';
2+
3+
export interface ErrorCatcherProps {
4+
renderFallback: () => React.ReactNode;
5+
}
6+
7+
export class ErrorCatcher extends React.Component<ErrorCatcherProps, { error: boolean }> {
8+
public state = { error: false };
9+
10+
public componentDidCatch(error: Error, info: ErrorInfo) {
11+
try {
12+
ga('send', 'event', 'error', {
13+
message: error.message,
14+
componentStack: info.componentStack,
15+
});
16+
} catch { /**/ }
17+
this.setState({ error: true });
18+
}
19+
20+
public render() {
21+
return this.state.error ? this.props.renderFallback() : this.props.children;
22+
}
23+
}

src/components/InteractiveCodeBlock/InteractiveCodeBlock.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ const buttonStyles = css([resets.unbutton], {
261261
top: rhythm(0.5),
262262
right: 0,
263263
background: 'var(--background)',
264-
...minWidth(variables.sizes.bigEnough, padding(1, Side.Right)),
264+
...minWidth(variables.sizes.bigEnough, { right: rhythm(1) }),
265265
});
266266

267267
function Button(props: React.ButtonHTMLAttributes<HTMLButtonElement>) {

src/templates/Post.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { TypeScriptDiagnosticToken } from '../components/InteractiveCodeBlock/Ty
1212
import 'katex/dist/katex.min.css';
1313
import { shallowClone } from '../utils/shallowClone';
1414
import SEO from '../components/SEO';
15+
import { ErrorCatcher } from '../components/ErrorCatcher';
1516

1617
const renderAst = new RehypeReact({
1718
createElement: React.createElement,
@@ -157,7 +158,11 @@ function ProgressiveCodeBlock(props: { children: [React.ReactElement<HTMLAttribu
157158
/>
158159
), { timeout: 1000 });
159160

160-
return deferredCodeBlock || <CheapCodeBlock>{text}</CheapCodeBlock>;
161+
return (
162+
<ErrorCatcher renderFallback={() => <CheapCodeBlock>{text}</CheapCodeBlock>}>
163+
{deferredCodeBlock || <CheapCodeBlock>{text}</CheapCodeBlock>}
164+
</ErrorCatcher>
165+
);
161166
}
162167

163168
type VirtualTypeScriptEnvironment = import('../utils/typescript/services').VirtualTypeScriptEnvironment;

0 commit comments

Comments
 (0)