Skip to content

Commit

Permalink
refactor(sanity): rename onStudioError to onUncaughtError
Browse files Browse the repository at this point in the history
  • Loading branch information
RitaDias committed Oct 7, 2024
1 parent c186d7c commit f498039
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions dev/test-studio/sanity.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ const defaultWorkspace = {
dataset: 'test',
plugins: [sharedSettings()],

onStudioError: (error, errorInfo) => {
onUncaughtError: (error, errorInfo) => {
// eslint-disable-next-line no-console
console.log(error)
// eslint-disable-next-line no-console
Expand Down Expand Up @@ -253,7 +253,7 @@ export default defineConfig([
dataset: 'test',
plugins: [sharedSettings(), studioComponentsPlugin(), formComponentsPlugin()],
basePath: '/custom-components',
onStudioError: (error, errorInfo) => {
onUncaughtError: (error, errorInfo) => {
// eslint-disable-next-line no-console
console.log(error)
// eslint-disable-next-line no-console
Expand Down
6 changes: 3 additions & 3 deletions packages/sanity/src/core/config/configPropertyReducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ export const documentCommentsEnabledReducer = (opts: {
return result
}

export const onStudioErrorResolver = (opts: {
export const onUncaughtErrorResolver = (opts: {
config: PluginOptions
context: {error: Error; errorInfo: ErrorInfo}
}) => {
Expand All @@ -320,13 +320,13 @@ export const onStudioErrorResolver = (opts: {
// There is no concept of 'previous value' in this API. We only care about the final value.
// That is, if a plugin returns true, but the next plugin returns false, the result will be false.
// The last plugin 'wins'.
const resolver = pluginConfig.onStudioError
const resolver = pluginConfig.onUncaughtError

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(
`Expected \`document.onUncaughtError\` to be a a function, but received ${getPrintableType(
resolver,
)}`,
)
Expand Down
6 changes: 3 additions & 3 deletions packages/sanity/src/core/config/prepareConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {
internalTasksReducer,
legacySearchEnabledReducer,
newDocumentOptionsResolver,
onStudioErrorResolver,
onUncaughtErrorResolver,
partialIndexingEnabledReducer,
resolveProductionUrlReducer,
schemaTemplatesReducer,
Expand Down Expand Up @@ -633,8 +633,8 @@ function resolveSource({
staticInitialValueTemplateItems,
options: config,
},
onStudioError: (error: Error, errorInfo: ErrorInfo) => {
return onStudioErrorResolver({
onUncaughtError: (error: Error, errorInfo: ErrorInfo) => {
return onUncaughtErrorResolver({
config,
context: {
error: error,
Expand Down
6 changes: 3 additions & 3 deletions packages/sanity/src/core/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,9 @@ export interface PluginOptions {
*/
beta?: BetaFeatures
/** Configuration for error handling.
* @internal
* @beta
*/
onStudioError?: (error: Error, errorInfo: ErrorInfo) => void
onUncaughtError?: (error: Error, errorInfo: ErrorInfo) => void
}

/** @internal */
Expand Down Expand Up @@ -788,7 +788,7 @@ export interface Source {
/** Configuration for error handling.
* @internal
*/
onStudioError?: (error: Error, errorInfo: ErrorInfo) => void
onUncaughtError?: (error: Error, errorInfo: ErrorInfo) => void
}

/** @internal */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ describe('FormBuilderInputErrorBoundary', () => {
expect(screen.getByTestId('child')).toBeInTheDocument()
})

it('calls onStudioError when an error is caught', async () => {
const onStudioError = jest.fn()
it('calls onUncaughtError when an error is caught', async () => {
const onUncaughtError = jest.fn()

const ThrowErrorComponent = () => {
throw new Error('An EXPECTED, testing error occurred!')
Expand All @@ -40,7 +40,7 @@ describe('FormBuilderInputErrorBoundary', () => {
name: 'default',
projectId: 'test',
dataset: 'test',
onStudioError,
onUncaughtError,
},
})

Expand All @@ -52,6 +52,6 @@ describe('FormBuilderInputErrorBoundary', () => {
</TestProvider>,
)

expect(onStudioError).toHaveBeenCalledTimes(1)
expect(onUncaughtError).toHaveBeenCalledTimes(1)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ describe('WorkspaceRouterProvider', () => {
expect(screen.getByText('Children')).toBeInTheDocument()
})

it('calls onStudioError when an error is caught', async () => {
const onStudioError = jest.fn()
it('calls onUncaughtError when an error is caught', async () => {
const onUncaughtError = jest.fn()

const ThrowErrorComponent = () => {
throw new Error('An EXPECTED, testing error occurred!')
Expand All @@ -84,7 +84,7 @@ describe('WorkspaceRouterProvider', () => {
name: 'default',
projectId: 'test',
dataset: 'test',
onStudioError,
onUncaughtError,
},
})

Expand All @@ -98,7 +98,7 @@ describe('WorkspaceRouterProvider', () => {
</TestProvider>,
)
} catch {
expect(onStudioError).toHaveBeenCalledTimes(1)
expect(onUncaughtError).toHaveBeenCalledTimes(1)
}
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {SourceContext} from '../../_singletons'
export type ErrorBoundaryProps = UIErrorBoundaryProps

/**
* ErrorBoundary component that catches errors and uses onStudioError config property
* ErrorBoundary component that catches errors and uses onUncaughtError config property
* It also calls the onCatch prop if it exists.
*/
export function ErrorBoundary({onCatch, ...rest}: ErrorBoundaryProps): JSX.Element {
Expand All @@ -19,8 +19,8 @@ export function ErrorBoundary({onCatch, ...rest}: ErrorBoundaryProps): JSX.Eleme

const handleCatch = useCallback(
({error: caughtError, info: caughtInfo}: {error: Error; info: React.ErrorInfo}) => {
// Send the error to the source if it has an onStudioError method
source?.onStudioError?.(caughtError, caughtInfo)
// Send the error to the source if it has an onUncaughtError method
source?.onUncaughtError?.(caughtError, caughtInfo)

// Call the onCatch prop if it exists
onCatch?.({error: caughtError, info: caughtInfo})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ describe('ErrorBoundary', () => {
jest.clearAllMocks()
})

it('calls onStudioError when an error is caught', async () => {
const onStudioError = jest.fn()
it('calls onUncaughtError when an error is caught', async () => {
const onUncaughtError = jest.fn()
const onCatch = jest.fn()

const ThrowErrorComponent = () => {
Expand All @@ -30,7 +30,7 @@ describe('ErrorBoundary', () => {
name: 'default',
projectId: 'test',
dataset: 'test',
onStudioError,
onUncaughtError,
},
})

Expand All @@ -42,10 +42,10 @@ describe('ErrorBoundary', () => {
</TestProvider>,
)

expect(onStudioError).toHaveBeenCalledTimes(1)
expect(onUncaughtError).toHaveBeenCalledTimes(1)
})

it('calls onCatch prop when an error is caught when no onStudioError exists', () => {
it('calls onCatch prop when an error is caught when no onUncaughtError exists', () => {
const onCatch = jest.fn()

const WrapperWithoutError = ({children}: {children: React.ReactNode}) => {
Expand Down

0 comments on commit f498039

Please sign in to comment.