-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export class DashboardDataError extends Error { | ||
constructor( | ||
public code: string, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leaving the code as 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ) | ||
); | ||
} |
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> | ||
); | ||
} |
There was a problem hiding this comment.
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