Skip to content

Commit

Permalink
fix: connectivity-related issues [LW 12346] (#1743)
Browse files Browse the repository at this point in the history
* fix: skip logging sentry connectivity error

* fix: add error boundary catching rendering errors
  • Loading branch information
szymonmaslowski authored Feb 27, 2025
1 parent 6c507c8 commit f5127ea
Show file tree
Hide file tree
Showing 17 changed files with 168 additions and 108 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React, { Component, ErrorInfo, ReactElement } from 'react';
import { logger } from '@lace/common';
import { removePreloaderIfExists } from '@utils/remove-reloader-if-exists';
import { Crash } from './Crash';

type ErrorBoundaryProps = {
children: ReactElement;
};
type ErrorBoundaryState = {
error?: Error;
};

export class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
static getDerivedStateFromError(error: Error): ErrorBoundaryState {
return { error };
}

state: ErrorBoundaryState = {};

componentDidCatch(error: Error, info: ErrorInfo): void {
removePreloaderIfExists();
logger.error('Caught by Error Boundary:', error, info.componentStack);
}

render(): ReactElement {
if (this.state.error) {
return <Crash />;
}
return this.props.children;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './Crash';
export { ErrorBoundary } from './ErrorBoundary';
57 changes: 30 additions & 27 deletions apps/browser-extension-wallet/src/dapp-connector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { getBackgroundStorage } from '@lib/scripts/background/storage';
import { NamiDappConnector } from './views/nami-mode/indexInternal';
import { storage } from 'webextension-polyfill';
import { TxWitnessRequestProvider } from '@providers/TxWitnessRequestProvider';
import { ErrorBoundary } from '@components/ErrorBoundary';

const App = (): React.ReactElement => {
const [mode, setMode] = useState<'lace' | 'nami' | undefined>();
Expand All @@ -44,33 +45,35 @@ const App = (): React.ReactElement => {
}, []);

return (
<BackgroundServiceAPIProvider>
<AppSettingsProvider>
<DatabaseProvider>
<StoreProvider appMode={APP_MODE_POPUP}>
<CurrencyStoreProvider>
<HashRouter>
<PostHogClientProvider>
<AnalyticsProvider>
<ThemeProvider>
<ExternalLinkOpenerProvider>
<AddressesDiscoveryOverlay>
<UIThemeProvider>
<TxWitnessRequestProvider>
{!mode ? <></> : mode === 'nami' ? <NamiDappConnector /> : <DappConnectorView />}
</TxWitnessRequestProvider>
</UIThemeProvider>
</AddressesDiscoveryOverlay>
</ExternalLinkOpenerProvider>
</ThemeProvider>
</AnalyticsProvider>
</PostHogClientProvider>
</HashRouter>
</CurrencyStoreProvider>
</StoreProvider>
</DatabaseProvider>
</AppSettingsProvider>
</BackgroundServiceAPIProvider>
<ErrorBoundary>
<BackgroundServiceAPIProvider>
<AppSettingsProvider>
<DatabaseProvider>
<StoreProvider appMode={APP_MODE_POPUP}>
<CurrencyStoreProvider>
<HashRouter>
<PostHogClientProvider>
<AnalyticsProvider>
<ThemeProvider>
<ExternalLinkOpenerProvider>
<AddressesDiscoveryOverlay>
<UIThemeProvider>
<TxWitnessRequestProvider>
{!mode ? <></> : mode === 'nami' ? <NamiDappConnector /> : <DappConnectorView />}
</TxWitnessRequestProvider>
</UIThemeProvider>
</AddressesDiscoveryOverlay>
</ExternalLinkOpenerProvider>
</ThemeProvider>
</AnalyticsProvider>
</PostHogClientProvider>
</HashRouter>
</CurrencyStoreProvider>
</StoreProvider>
</DatabaseProvider>
</AppSettingsProvider>
</BackgroundServiceAPIProvider>
</ErrorBoundary>
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Portal } from '@views/browser/features/wallet-setup/components/Portal';
import { useAnalyticsContext } from '@providers';
import { postHogNamiMigrationActions } from '@providers/AnalyticsProvider/analyticsTracker';
import { useFatalError } from '@hooks/useFatalError';
import { Crash } from '@components/Crash';
import { Crash } from '@components/ErrorBoundary';

const urlPath = walletRoutePaths.namiMigration;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ const GATEWAY_TIMEOUT_STATUS_CODE = 503;
const UNAUTHORIZED_STATUS_CODE = 401;
const NOT_FOUND_STATUS_CODE = 404;

// eslint-disable-next-line unicorn/no-null
let sentryUrl: string | null = '';
const sentryUrlExtractionRegex = /https:\/\/[^@]+@([^/]+).*/;
if (sentryUrlExtractionRegex.test(process.env.SENTRY_DSN)) {
sentryUrl = process.env.SENTRY_DSN.replace(sentryUrlExtractionRegex, 'https://$1');
}

const isSentryRequest = (url: string) => !!sentryUrl && url.startsWith(sentryUrl);

const handleProviderServerErrors = (data: WebRequest.OnCompletedDetailsType) => {
if (data?.type === 'xmlhttprequest' && runtime.getURL('').startsWith(data.initiator)) {
const statusCodeQualifiedAsFailure =
Expand Down Expand Up @@ -46,7 +55,9 @@ const handleConnectionIssues = async (error: WebRequest.OnErrorOccurredDetailsTy
)
return;

logger.debug('xmlhttprequest:net::ERR_INTERNET_DISCONNECTED', error);
if (!isSentryRequest(error.url)) {
logger.error('xmlhttprequest:net::ERR_INTERNET_DISCONNECTED', error);
}

requestMessage$.next({ type: MessageTypes.HTTP_CONNECTION, data: { connected: false } });
if (!webRequest.onCompleted.hasListener(handleRequests)) {
Expand Down
65 changes: 34 additions & 31 deletions apps/browser-extension-wallet/src/popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { NamiMigrationGuard } from './features/nami-migration/NamiMigrationGuard
import { createNonBackgroundMessenger } from '@cardano-sdk/web-extension';
import { logger } from '@lace/common';
import { AppVersionGuard } from './utils/AppVersionGuard';
import { ErrorBoundary } from '@components/ErrorBoundary';

const App = (): React.ReactElement => {
const [mode, setMode] = useState<'lace' | 'nami'>();
Expand All @@ -51,37 +52,39 @@ const App = (): React.ReactElement => {
}, [mode]);

return (
<BackgroundServiceAPIProvider>
<AppSettingsProvider>
<DatabaseProvider>
<StoreProvider appMode={APP_MODE_POPUP}>
<CurrencyStoreProvider>
<HashRouter>
<PostHogClientProvider>
<AnalyticsProvider>
<ThemeProvider>
<ExternalLinkOpenerProvider>
<MigrationContainer appMode={APP_MODE_POPUP}>
<DataCheckContainer appMode={APP_MODE_POPUP}>
<AddressesDiscoveryOverlay>
<NamiMigrationGuard>
<BackgroundPageProvider>
<AppVersionGuard>{mode === 'nami' ? <NamiPopup /> : <PopupView />}</AppVersionGuard>
</BackgroundPageProvider>
</NamiMigrationGuard>
</AddressesDiscoveryOverlay>
</DataCheckContainer>
</MigrationContainer>
</ExternalLinkOpenerProvider>
</ThemeProvider>
</AnalyticsProvider>
</PostHogClientProvider>
</HashRouter>
</CurrencyStoreProvider>
</StoreProvider>
</DatabaseProvider>
</AppSettingsProvider>
</BackgroundServiceAPIProvider>
<ErrorBoundary>
<BackgroundServiceAPIProvider>
<AppSettingsProvider>
<DatabaseProvider>
<StoreProvider appMode={APP_MODE_POPUP}>
<CurrencyStoreProvider>
<HashRouter>
<PostHogClientProvider>
<AnalyticsProvider>
<ThemeProvider>
<ExternalLinkOpenerProvider>
<MigrationContainer appMode={APP_MODE_POPUP}>
<DataCheckContainer appMode={APP_MODE_POPUP}>
<AddressesDiscoveryOverlay>
<NamiMigrationGuard>
<BackgroundPageProvider>
<AppVersionGuard>{mode === 'nami' ? <NamiPopup /> : <PopupView />}</AppVersionGuard>
</BackgroundPageProvider>
</NamiMigrationGuard>
</AddressesDiscoveryOverlay>
</DataCheckContainer>
</MigrationContainer>
</ExternalLinkOpenerProvider>
</ThemeProvider>
</AnalyticsProvider>
</PostHogClientProvider>
</HashRouter>
</CurrencyStoreProvider>
</StoreProvider>
</DatabaseProvider>
</AppSettingsProvider>
</BackgroundServiceAPIProvider>
</ErrorBoundary>
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ import { tabs } from 'webextension-polyfill';
import { useTranslation } from 'react-i18next';
import { DappSignDataSuccess } from '@src/features/dapp/components/DappSignDataSuccess';
import { DappSignDataFail } from '@src/features/dapp/components/DappSignDataFail';
import { Crash } from '@components/Crash';
import { Crash } from '@components/ErrorBoundary';
import { useFatalError } from '@hooks/useFatalError';
import { POPUP_WINDOW } from '@src/utils/constants';
import { removePreloaderIfExists } from '@utils/remove-reloader-if-exists';

dayjs.extend(duration);

Expand Down Expand Up @@ -71,7 +72,7 @@ export const DappConnectorView = (): React.ReactElement => {
const fatalError = useFatalError();
useEffect(() => {
if (!isLoading || fatalError) {
document.querySelector('#preloader')?.remove();
removePreloaderIfExists();
}
}, [isLoading, fatalError]);

Expand Down
5 changes: 3 additions & 2 deletions apps/browser-extension-wallet/src/routes/PopupView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { MainLoader } from '@components/MainLoader';
import { useAppInit } from '@hooks';
import { ILocalStorage } from '@src/types';
import { useFatalError } from '@hooks/useFatalError';
import { Crash } from '@components/Crash';
import { Crash } from '@components/ErrorBoundary';
import { removePreloaderIfExists } from '@utils/remove-reloader-if-exists';

dayjs.extend(duration);

Expand Down Expand Up @@ -64,7 +65,7 @@ export const PopupView = (): React.ReactElement => {
);
useEffect(() => {
if (isLoaded || fatalError) {
document.querySelector('#preloader')?.remove();
removePreloaderIfExists();
}
}, [isLoaded, fatalError]);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const removePreloaderIfExists = (): void => {
document.querySelector('#preloader')?.remove();
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { useAnalyticsContext } from '@providers';
import { PostHogAction } from '@providers/AnalyticsProvider/analyticsTracker';
import { useWalletActivitiesPaginated } from '@hooks/useWalletActivities';

const loadMoreDebounce = 300;
const loadMoreDebounceTime = 300;

export const ActivityLayout = (): ReactElement => {
const { t } = useTranslation();
Expand Down Expand Up @@ -72,7 +72,7 @@ export const ActivityLayout = (): ReactElement => {

const isLoadingFirstTime = isNil(total);

const debouncedLoadMore = useMemo(() => debounce(loadMore, loadMoreDebounce), [loadMore]);
const debouncedLoadMore = useMemo(() => debounce(loadMore, loadMoreDebounceTime), [loadMore]);

return (
<Layout>
Expand Down
73 changes: 38 additions & 35 deletions apps/browser-extension-wallet/src/views/browser-view/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,43 +29,46 @@ import { PostHogClientProvider } from '@providers/PostHogClientProvider';
import { AddressesDiscoveryOverlay } from 'components/AddressesDiscoveryOverlay';
import { NamiMigrationGuard } from '@src/features/nami-migration/NamiMigrationGuard';
import { AppVersionGuard } from '@src/utils/AppVersionGuard';
import { ErrorBoundary } from '@components/ErrorBoundary';

const App = (): React.ReactElement => (
<BackgroundServiceAPIProvider>
<AppSettingsProvider>
<DatabaseProvider>
<StoreProvider appMode={APP_MODE_BROWSER}>
<CurrencyStoreProvider>
<HashRouter>
<BackgroundPageProvider>
<PostHogClientProvider>
<AnalyticsProvider>
<ThemeProvider>
<UIThemeProvider>
<ExternalLinkOpenerProvider>
<MigrationContainer appMode={APP_MODE_BROWSER}>
<DataCheckContainer appMode={APP_MODE_BROWSER}>
<AddressesDiscoveryOverlay>
<NamiMigrationGuard>
<AppVersionGuard>
<BrowserViewRoutes />
</AppVersionGuard>
</NamiMigrationGuard>
</AddressesDiscoveryOverlay>
</DataCheckContainer>
</MigrationContainer>
</ExternalLinkOpenerProvider>
</UIThemeProvider>
</ThemeProvider>
</AnalyticsProvider>
</PostHogClientProvider>
</BackgroundPageProvider>
</HashRouter>
</CurrencyStoreProvider>
</StoreProvider>
</DatabaseProvider>
</AppSettingsProvider>
</BackgroundServiceAPIProvider>
<ErrorBoundary>
<BackgroundServiceAPIProvider>
<AppSettingsProvider>
<DatabaseProvider>
<StoreProvider appMode={APP_MODE_BROWSER}>
<CurrencyStoreProvider>
<HashRouter>
<BackgroundPageProvider>
<PostHogClientProvider>
<AnalyticsProvider>
<ThemeProvider>
<UIThemeProvider>
<ExternalLinkOpenerProvider>
<MigrationContainer appMode={APP_MODE_BROWSER}>
<DataCheckContainer appMode={APP_MODE_BROWSER}>
<AddressesDiscoveryOverlay>
<NamiMigrationGuard>
<AppVersionGuard>
<BrowserViewRoutes />
</AppVersionGuard>
</NamiMigrationGuard>
</AddressesDiscoveryOverlay>
</DataCheckContainer>
</MigrationContainer>
</ExternalLinkOpenerProvider>
</UIThemeProvider>
</ThemeProvider>
</AnalyticsProvider>
</PostHogClientProvider>
</BackgroundPageProvider>
</HashRouter>
</CurrencyStoreProvider>
</StoreProvider>
</DatabaseProvider>
</AppSettingsProvider>
</BackgroundServiceAPIProvider>
</ErrorBoundary>
);

const mountNode = document.querySelector('#lace-app');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ import { Trans, useTranslation } from 'react-i18next';
import { POPUP_WINDOW_NAMI_TITLE } from '@src/utils/constants';
import { DAppExplorer } from '@views/browser/features/dapp/explorer/components/DAppExplorer';
import { useFatalError } from '@hooks/useFatalError';
import { Crash } from '@components/Crash';
import { Crash } from '@components/ErrorBoundary';
import { useIsPosthogClientInitialized } from '@providers/PostHogClientProvider/useIsPosthogClientInitialized';
import { logger } from '@lace/common';
import { VotingLayout } from '../features/voting-beta';
import { catchAndBrandExtensionApiError } from '@utils/catch-and-brand-extension-api-error';
import { removePreloaderIfExists } from '@utils/remove-reloader-if-exists';

export const defaultRoutes: RouteMap = [
{
Expand Down Expand Up @@ -241,7 +242,7 @@ export const BrowserViewRoutes = ({ routesMap = defaultRoutes }: { routesMap?: R

useEffect(() => {
if (isLoaded || isOnboarding || isInNamiMode || fatalError) {
document.querySelector('#preloader')?.remove();
removePreloaderIfExists();
}
}, [isLoaded, isOnboarding, isInNamiMode, fatalError]);

Expand Down
Loading

0 comments on commit f5127ea

Please sign in to comment.