Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(js-ts): Convert app/components/UI/ComponentErrorBoundary/index.js to TypeScript #11271

Merged
merged 3 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 0 additions & 58 deletions app/components/UI/ComponentErrorBoundary/index.js

This file was deleted.

57 changes: 57 additions & 0 deletions app/components/UI/ComponentErrorBoundary/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React from 'react';
import Logger from '../../../util/Logger';
import trackErrorAsAnalytics from '../../../util/metrics/TrackError/trackErrorAsAnalytics';

interface ComponentErrorBoundaryProps {
/**
* Component to be used when there is no error
*/
children: React.ReactNode;
/**
* Component label for logging
*/
componentLabel: string;
/**
* Function to be called when there is an error
*/
onError?: () => void;
/**
* Will not track as an error, but still log to analytics
*/
dontTrackAsError?: boolean;
}

interface ComponentErrorBoundaryState {
error: Error | null;
}

class ComponentErrorBoundary extends React.Component<ComponentErrorBoundaryProps, ComponentErrorBoundaryState> {
state: ComponentErrorBoundaryState = { error: null };

static getDerivedStateFromError(error: Error): ComponentErrorBoundaryState {
return { error };
}

componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void {
this.props.onError?.();

const { componentLabel, dontTrackAsError } = this.props;

if (dontTrackAsError) {
return trackErrorAsAnalytics(
`Component Error Boundary: ${componentLabel}`,
error?.message,
);
}
Logger.error(error, { View: this.props.componentLabel, ...errorInfo });
}

getErrorMessage = (): string =>
`Component: ${this.props.componentLabel}\n${this.state.error?.toString()}`;

render(): React.ReactNode {
return this.state.error ? null : this.props.children;
}
}

export default ComponentErrorBoundary;
Loading