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

[No QA] [TS Migration] Migrate 'ErrorBoundary' component to TypeScript #30337

Merged
merged 6 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
import PropTypes from 'prop-types';
import React from 'react';
import {ErrorBoundary} from 'react-error-boundary';
import BootSplash from '@libs/BootSplash';
import GenericErrorPage from '@pages/ErrorPage/GenericErrorPage';

const propTypes = {
/* A message posted to `logError` (along with error data) when this component intercepts an error */
errorMessage: PropTypes.string.isRequired,

/* A function to perform the actual logging since different platforms support different tools */
logError: PropTypes.func,

/* Actual content wrapped by this error boundary */
children: PropTypes.node.isRequired,
};

const defaultProps = {
logError: () => {},
};
import {BaseErrorBoundaryProps, LogError} from './types';

/**
* This component captures an error in the child component tree and logs it to the server
* It can be used to wrap the entire app as well as to wrap specific parts for more granularity
* @see {@link https://reactjs.org/docs/error-boundaries.html#where-to-place-error-boundaries}
* @return {React.Component}
*/
function BaseErrorBoundary({logError, errorMessage, children}) {
const catchError = (error, errorInfo) => {

function BaseErrorBoundary({logError = () => {}, errorMessage, children}: BaseErrorBoundaryProps) {
const catchError = (error: Error, errorInfo: React.ErrorInfo) => {
logError(errorMessage, error, JSON.stringify(errorInfo));
// We hide the splash screen since the error might happened during app init
BootSplash.hide();
Expand All @@ -42,8 +27,6 @@ function BaseErrorBoundary({logError, errorMessage, children}) {
);
}

BaseErrorBoundary.propTypes = propTypes;
BaseErrorBoundary.defaultProps = defaultProps;
BaseErrorBoundary.displayName = 'BaseErrorBoundary';

export type {LogError, BaseErrorBoundaryProps};
export default BaseErrorBoundary;
8 changes: 0 additions & 8 deletions src/components/ErrorBoundary/index.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import crashlytics from '@react-native-firebase/crashlytics';
import Log from '@libs/Log';
import BaseErrorBoundary from './BaseErrorBoundary';
import {BaseErrorBoundaryProps, LogError} from './types';

BaseErrorBoundary.defaultProps.logError = (errorMessage, error, errorInfo) => {
const logError: LogError = (errorMessage, error, errorInfo) => {
// Log the error to the server
Log.alert(`${errorMessage} - ${error.message}`, {errorInfo}, false);

Expand All @@ -12,4 +13,17 @@ BaseErrorBoundary.defaultProps.logError = (errorMessage, error, errorInfo) => {
crashlytics().recordError(error);
};

export default BaseErrorBoundary;
function ErrorBoundary({errorMessage, children}: Omit<BaseErrorBoundaryProps, 'logError'>) {
return (
<BaseErrorBoundary
errorMessage={errorMessage}
logError={logError}
>
{children}
</BaseErrorBoundary>
);
}

ErrorBoundary.displayName = 'ErrorBoundary';

export default ErrorBoundary;
23 changes: 23 additions & 0 deletions src/components/ErrorBoundary/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Log from '@libs//Log';
import BaseErrorBoundary from './BaseErrorBoundary';
import {BaseErrorBoundaryProps, LogError} from './types';

const logError: LogError = (errorMessage, error, errorInfo) => {
// Log the error to the server
Log.alert(`${errorMessage} - ${error.message}`, {errorInfo}, false);
};

function ErrorBoundary({errorMessage, children}: Omit<BaseErrorBoundaryProps, 'logError'>) {
return (
<BaseErrorBoundary
errorMessage={errorMessage}
logError={logError}
>
{children}
</BaseErrorBoundary>
);
}

ErrorBoundary.displayName = 'ErrorBoundary';

export default ErrorBoundary;
14 changes: 14 additions & 0 deletions src/components/ErrorBoundary/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
type LogError = (message: string, error: Error, errorInfo: string) => void;

type BaseErrorBoundaryProps = {
/* A message posted to `logError` (along with error data) when this component intercepts an error */
errorMessage: string;

/* A function to perform the actual logging since different platforms support different tools */
logError?: LogError;

/* Actual content wrapped by this error boundary */
children: React.ReactNode;
};

export type {BaseErrorBoundaryProps, LogError};
Loading