Skip to content

Commit 375095c

Browse files
committed
refactor(errors): improve typings for global error handler
1 parent b2b742d commit 375095c

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './GlobalErrorHandler'
2+
export * from './types'
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export interface GlobalErrorMessage {
2+
error: Error | null
3+
params: {
4+
colno: number
5+
lineno: number
6+
error: Error
7+
event: ErrorEvent | string
8+
source?: string
9+
}
10+
}
11+
12+
export type GlobalErrorSubscriber = (msg: GlobalErrorMessage) => void
13+
export type GlobalErrorUnsubscriber = () => void
14+
15+
export interface GlobalErrorChannel {
16+
subscribe: (subscriber: GlobalErrorSubscriber) => GlobalErrorUnsubscriber
17+
}

packages/sanity/src/studio/StudioErrorBoundary.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ interface StudioErrorBoundaryProps {
99
children: React.ReactNode
1010
}
1111

12-
const errorChannel = (globalScope as any).__sanityErrorChannel
12+
const errorChannel = globalScope.__sanityErrorChannel
1313

14-
function isKnownError(err: unknown): boolean {
14+
function isKnownError(err: Error): boolean {
1515
if (err instanceof SchemaError) {
1616
return true
1717
}
@@ -29,10 +29,15 @@ export function StudioErrorBoundary({children}: StudioErrorBoundaryProps) {
2929
useEffect(() => {
3030
if (!errorChannel) return undefined
3131

32-
return errorChannel.subscribe((msg: any) => {
32+
return errorChannel.subscribe((msg) => {
3333
// NOTE: Certain errors (such as the `ResizeObserver loop limit exceeded` error) is thrown
34-
// by the browser, and does not include an `error` property. We ignore these error.
35-
if (!msg.error || isKnownError(msg.error)) {
34+
// by the browser, and does not include an `error` property. We ignore these errors.
35+
if (!msg.error) {
36+
return
37+
}
38+
39+
// For errors that we "expect", eg have specific error screens for, do not push a toast
40+
if (isKnownError(msg.error)) {
3641
return
3742
}
3843

packages/sanity/src/util/globalScope.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type {GlobalErrorChannel} from '../components/globalErrorHandler'
2+
13
/**
24
* Gets the global scope instance in a given environment.
35
*
@@ -7,7 +9,7 @@
79
* - The `self` variable is the global scope in workers and others
810
* - The `global` variable is the global scope in Node.js
911
*/
10-
function getGlobalScope() {
12+
function getGlobalScope(): typeof globalThis & {__sanityErrorChannel?: GlobalErrorChannel} {
1113
if (typeof globalThis !== 'undefined') return globalThis
1214
if (typeof window !== 'undefined') return window
1315
if (typeof self !== 'undefined') return self

0 commit comments

Comments
 (0)