Skip to content

Commit

Permalink
feat(sanity): add call on FormBuilderInput, add ability to have confi…
Browse files Browse the repository at this point in the history
…g as undefined
  • Loading branch information
RitaDias committed Sep 27, 2024
1 parent 9297864 commit 92b6071
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions dev/test-studio/components/studioComponents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const useTitleContext = () => useContext(TitleContext)
export function CustomLayout(props: LayoutProps) {
const {renderDefault} = props

throw new Error('This is an error from the custom layout')
return (
<TitleContext.Provider value="Context value">
<Box height="fill" data-testid="test-layout-config">
Expand Down
14 changes: 7 additions & 7 deletions dev/test-studio/sanity.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,6 @@ const sharedSettings = definePlugin({
},
},

onStudioError: (error, errorInfo) => {
// eslint-disable-next-line no-console
console.log(error)
// eslint-disable-next-line no-console
console.log(errorInfo)
},

document: {
actions: documentActions,
inspectors: (prev, ctx) => {
Expand Down Expand Up @@ -160,6 +153,13 @@ const defaultWorkspace = {
projectId: 'ppsg7ml5',
dataset: 'test',
plugins: [sharedSettings()],

onStudioError: (error, errorInfo) => {
// eslint-disable-next-line no-console
console.log(error)
// eslint-disable-next-line no-console
console.log(errorInfo)
},
basePath: '/test',
icon: SanityMonogram,
// eslint-disable-next-line camelcase
Expand Down
1 change: 1 addition & 0 deletions packages/sanity/src/core/config/configPropertyReducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ export const onStudioErrorResolver = (opts: {
const resolver = config.onStudioError

if (typeof resolver === 'function') return resolver(context.error, context.errorInfo)
if (!resolver) return undefined

throw new Error(
`Expected \`document.onStudioError\` to be a a function, but received ${getPrintableType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ function BaseImageInputComponent(props: BaseImageInputProps): JSX.Element {
}, [menuButtonElement])

const renderPreview = useCallback(() => {
throw new Error('This is an error from an image!')

return (
<ImageInputPreview
directUploads={directUploads}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Box, Card, Code, ErrorBoundary, Stack, Text} from '@sanity/ui'
import {useCallback, useMemo, useState} from 'react'
import {useSource} from 'sanity'
import {useHotModuleReload} from 'use-hot-module-reload'

import {SchemaError} from '../../config'
Expand Down Expand Up @@ -27,10 +28,21 @@ export function FormBuilderInputErrorBoundary(
error: null,
info: {},
})
const {onStudioError} = useSource()
const handleRetry = useCallback(() => setError({error: null, info: {}}), [])
const handleCatch = useCallback(
({error: caughtError, info: caughtInfo}: {error: Error; info: React.ErrorInfo}) => {
setError({error: caughtError, info: caughtInfo})

if (onStudioError) {
onStudioError(caughtError, caughtInfo)
}
},
[onStudioError],
)

if (!error) {
return <ErrorBoundary onCatch={setError}>{children}</ErrorBoundary>
return <ErrorBoundary onCatch={handleCatch}>{children}</ErrorBoundary>
}

return <ErrorCard error={error} info={info} onRetry={handleRetry} />
Expand Down

0 comments on commit 92b6071

Please sign in to comment.