8
8
withScope ,
9
9
} from '@sentry/browser' ;
10
10
import { Event } from '@sentry/types' ;
11
- import { parseSemver } from '@sentry/utils' ;
11
+ import { logger , parseSemver } from '@sentry/utils' ;
12
12
import hoistNonReactStatics from 'hoist-non-react-statics' ;
13
13
import * as React from 'react' ;
14
14
@@ -21,7 +21,7 @@ export type FallbackRender = (errorData: {
21
21
componentStack : string | null ;
22
22
eventId : string | null ;
23
23
resetError ( ) : void ;
24
- } ) => React . ReactNode ;
24
+ } ) => React . ReactElement ;
25
25
26
26
export type ErrorBoundaryProps = {
27
27
/** If a Sentry report dialog should be rendered on error */
@@ -39,7 +39,7 @@ export type ErrorBoundaryProps = {
39
39
* the error, the component stack, and an function that resets the error boundary on error.
40
40
*
41
41
*/
42
- fallback ?: React . ReactNode | FallbackRender ;
42
+ fallback ?: React . ReactElement | FallbackRender ;
43
43
/** Called with the error boundary encounters an error */
44
44
onError ?( error : Error , componentStack : string , eventId : string ) : void ;
45
45
/** Called on componentDidMount() */
@@ -160,14 +160,22 @@ class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundarySta
160
160
const { error, componentStack, eventId } = this . state ;
161
161
162
162
if ( error ) {
163
- if ( React . isValidElement ( fallback ) ) {
164
- return fallback ;
165
- }
163
+ let element : React . ReactElement | undefined = undefined ;
166
164
if ( typeof fallback === 'function' ) {
167
- return fallback ( { error, componentStack, resetError : this . resetErrorBoundary , eventId } ) as FallbackRender ;
165
+ element = fallback ( { error, componentStack, resetError : this . resetErrorBoundary , eventId } ) ;
166
+ } else {
167
+ element = fallback ;
168
+ }
169
+
170
+ if ( React . isValidElement ( element ) ) {
171
+ return element ;
172
+ }
173
+
174
+ if ( fallback ) {
175
+ logger . warn ( 'fallback did not produce a valid ReactElement' ) ;
168
176
}
169
177
170
- // Fail gracefully if no fallback provided
178
+ // Fail gracefully if no fallback provided or is not valid
171
179
return null ;
172
180
}
173
181
0 commit comments