Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
feat(component): add error wrapper around preview components
Browse files Browse the repository at this point in the history
  • Loading branch information
Lasse Küchler committed Dec 19, 2017
1 parent 0f2ad2b commit 0325ea6
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/component/presentation/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ export class Preview extends React.Component<PreviewProps> {
patternFactory = require(patternPath).default;
this.patternFactories[patternPath] = patternFactory;
}
const reactComponent = patternFactory(componentProps);

// Finally, build the component
return patternFactory(componentProps);
return <PatternWrapper key={key}>{reactComponent}</PatternWrapper>;
} else {
// The model is an object, but not a pattern declaration.
// Create a new object with recursively processed values.
Expand All @@ -111,3 +112,26 @@ export class Preview extends React.Component<PreviewProps> {
}
}
}

interface PatternWrapperState {
errorMessage?: string;
}

class PatternWrapper extends React.Component<{}, PatternWrapperState> {
public constructor(props: {}) {
super(props);
this.state = {};
}

public componentDidCatch(error: Error): void {
this.setState({ errorMessage: error.message });
}

public render(): React.ReactNode {
if (this.state.errorMessage) {
return <span>{this.state.errorMessage}</span>;
} else {
return this.props.children;
}
}
}

0 comments on commit 0325ea6

Please sign in to comment.