Skip to content

Detect errors in data layer and handle them when routing #104920

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

Merged
merged 3 commits into from
Jul 28, 2025
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
2 changes: 2 additions & 0 deletions client/dashboard/app/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
createRootRoute,
redirect,
createLazyRoute,
lazyRouteComponent,
} from '@tanstack/react-router';
import { HostingFeatures } from '../data/constants';
import { fetchTwoStep } from '../data/me';
Expand Down Expand Up @@ -110,6 +111,7 @@ const siteRoute = createRoute( {
await queryClient.ensureQueryData( siteByIdQuery( otherEnvironmentSiteId ) );
}
},
errorComponent: lazyRouteComponent( () => import( '../sites/site/error' ) ),
} ).lazy( () =>
import( '../sites/site' ).then( ( d ) =>
createLazyRoute( 'site' )( {
Expand Down
31 changes: 31 additions & 0 deletions client/dashboard/data/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export class DashboardDataError extends Error {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By making our own type we can do instanceof DashboardDataError

constructor(
public code: string,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving the code as string seems ok to me. Right? We don't have a need to do exhaustive matching—we don't need to be sure the component at sites/site/error.tsx handles every since code—so I think there isn't much use in having a giant type definition that includes every single error case that every single API can return.

Throw error with generic string, catch error with generic string. Seems greppable :)

cause?: unknown
) {
const message = cause instanceof Error ? cause.message : `Error: ${ cause }`;
super( message, { cause } );
this.name = 'DashboardDataError';

// Fix prototype chain (important for instanceof to work reliably)
Object.setPrototypeOf( this, new.target.prototype );
Comment on lines +10 to +11
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI suggested this. Apparently in some transpiling situations can break the prototype chain.

}
}

interface WPError extends Error {
status: number;
statusCode: number;
error?: string;
[ key: string ]: unknown;
}

export function isWpError( error: unknown ): error is WPError {
return (
error instanceof Error &&
'status' in error &&
typeof error.status === 'number' &&
'statusCode' in error &&
typeof error.statusCode === 'number' &&
( 'error' in error ? typeof error.error === 'string' : true )
);
}
16 changes: 12 additions & 4 deletions client/dashboard/data/site.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import wpcom from 'calypso/lib/wp';
import { isWpError, DashboardDataError } from './error';

export const SITE_FIELDS = [
'ID',
Expand Down Expand Up @@ -112,10 +113,17 @@ export interface Site {
}

export async function fetchSite( siteIdOrSlug: number | string ): Promise< Site > {
return await wpcom.req.get(
{ path: `/sites/${ siteIdOrSlug }` },
{ fields: JOINED_SITE_FIELDS, options: JOINED_SITE_OPTIONS }
);
try {
return await wpcom.req.get(
{ path: `/sites/${ siteIdOrSlug }` },
{ fields: JOINED_SITE_FIELDS, options: JOINED_SITE_OPTIONS }
);
} catch ( error ) {
if ( isWpError( error ) && error.error === 'parse_error' ) {
throw new DashboardDataError( 'inaccessible_jetpack', error );
}
throw error;
}
}

export async function deleteSite( siteId: number ) {
Expand Down
42 changes: 42 additions & 0 deletions client/dashboard/sites/site/error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Notice } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import UnknownError from '../../app/500';
import { siteRoute } from '../../app/router';
import { PageHeader } from '../../components/page-header';
import PageLayout from '../../components/page-layout';
import RouterLinkButton from '../../components/router-link-button';
import { DashboardDataError } from '../../data/error';

export default function Error( { error }: { error: Error } ) {
switch ( error instanceof DashboardDataError && error.code ) {
case 'inaccessible_jetpack':
return <InaccessibleJetpackError error={ error } />;
default:
return <UnknownError error={ error } />;
}
}

function InaccessibleJetpackError( { error }: { error: Error } ) {
const { siteSlug } = siteRoute.useParams();

return (
<PageLayout
header={
<PageHeader
title={ siteSlug }
description={ __( 'Your Jetpack site can not be reached at this time.' ) }
actions={
<RouterLinkButton to="/sites" variant="primary" __next40pxDefaultSize>
{ __( 'Go to Sites' ) }
</RouterLinkButton>
}
/>
}
notices={
<Notice status="error" isDismissible={ false }>
{ error.message }
</Notice>
}
></PageLayout>
);
}