-
Notifications
You must be signed in to change notification settings - Fork 417
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
131 additions
and
120 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
47 changes: 47 additions & 0 deletions
47
packages/sanity/src/core/components/errorActions/ErrorActions.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {CopyIcon, SyncIcon} from '@sanity/icons' | ||
import {Inline} from '@sanity/ui' | ||
import {type ComponentProps, type ComponentType} from 'react' | ||
|
||
import {Button, Tooltip} from '../../../ui-components' | ||
import {strings} from './strings' | ||
import {useCopyErrorDetails} from './useCopyErrorDetails' | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export interface ErrorActionsProps extends Pick<ComponentProps<typeof Button>, 'size'> { | ||
error: unknown | ||
eventId: string | null | ||
onRetry?: () => void | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const ErrorActions: ComponentType<ErrorActionsProps> = ({error, eventId, onRetry, size}) => { | ||
const copyErrorDetails = useCopyErrorDetails(error, eventId) | ||
|
||
return ( | ||
<Inline space={3}> | ||
{onRetry && ( | ||
<Button | ||
onClick={onRetry} | ||
text={strings['retry.title']} | ||
tone="primary" | ||
icon={SyncIcon} | ||
size={size} | ||
/> | ||
)} | ||
<Tooltip content={strings['copy-error-details.description']}> | ||
<Button | ||
onClick={copyErrorDetails} | ||
text={strings['copy-error-details.title']} | ||
tone="default" | ||
mode="ghost" | ||
icon={CopyIcon} | ||
size={size} | ||
/> | ||
</Tooltip> | ||
</Inline> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './ErrorActions' | ||
export * from './types' | ||
export * from './useCopyErrorDetails' |
10 changes: 10 additions & 0 deletions
10
packages/sanity/src/core/components/errorActions/strings.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// These strings are not internationalized because `ErrorActions` is used inside | ||
// `StudioErrorBoundary`, which is rendered outside of `LocaleProvider`. | ||
export const strings = { | ||
'retry.title': 'Retry', | ||
'copy-error-details.description': 'These technical details may be useful for developers.', | ||
'copy-error-details.title': 'Copy error details', | ||
'copy-error-details.toast.get-failed': 'Failed to get error details', | ||
'copy-error-details.toast.copy-failed': 'Failed to copy error details', | ||
'copy-error-details.toast.succeeded': 'Copied error details to clipboard', | ||
} as const |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* @internal | ||
*/ | ||
export interface ErrorWithId { | ||
error: unknown | ||
eventId?: string | null | ||
} |
3 changes: 2 additions & 1 deletion
3
.../src/core/components/ErrorActions.test.ts → .../errorActions/useCopyErrorDetails.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
packages/sanity/src/core/components/errorActions/useCopyErrorDetails.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import {useToast} from '@sanity/ui' | ||
import {pick} from 'lodash' | ||
import {useCallback} from 'react' | ||
import {catchError, EMPTY, map, of, type OperatorFunction, tap} from 'rxjs' | ||
|
||
import {isRecord} from '../../util' | ||
import {strings} from './strings' | ||
import {type ErrorWithId} from './types' | ||
|
||
const TOAST_ID = 'copyErrorDetails' | ||
|
||
export function useCopyErrorDetails(error: unknown, eventId?: string | null): () => void { | ||
const toast = useToast() | ||
|
||
return useCallback(() => { | ||
of<ErrorWithId>({error, eventId}) | ||
.pipe( | ||
serializeError(), | ||
catchError((serializeErrorError) => { | ||
console.error(serializeErrorError) | ||
toast.push({ | ||
status: 'error', | ||
title: strings['copy-error-details.toast.get-failed'], | ||
id: TOAST_ID, | ||
}) | ||
return EMPTY | ||
}), | ||
tap((errorDetailsString) => { | ||
navigator.clipboard.writeText(errorDetailsString) | ||
toast.push({ | ||
status: 'success', | ||
title: strings['copy-error-details.toast.succeeded'], | ||
id: TOAST_ID, | ||
}) | ||
}), | ||
catchError((copyErrorError) => { | ||
console.error(copyErrorError) | ||
toast.push({ | ||
status: 'error', | ||
title: strings['copy-error-details.toast.copy-failed'], | ||
id: TOAST_ID, | ||
}) | ||
return EMPTY | ||
}), | ||
) | ||
.subscribe() | ||
}, [error, eventId, toast]) | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function serializeError(): OperatorFunction<ErrorWithId, string> { | ||
return map<ErrorWithId, string>(({error, eventId}) => { | ||
// Extract the non-enumerable properties of the provided `error` object. This is particularly | ||
// useful if the provided `error` value is an instance of `Error`, whose properties are | ||
// non-enumerable. | ||
const errorInfo = isRecord(error) ? pick(error, Object.getOwnPropertyNames(error)) : undefined | ||
return JSON.stringify({error: errorInfo, eventId}, null, 2) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters