55 * LICENSE file in the root directory of this source tree.
66 *
77 * @format
8+ * @flow strict
89 * @polyfill
9- * @nolint
1010 */
1111
1212let _inGuard = 0 ;
1313
14+ type ErrorHandler = ( error : mixed , isFatal : boolean ) => void ;
15+ type Fn < Args , Return > = ( ...Args ) => Return ;
16+
1417/**
1518 * This is the error handler that is called when we encounter an exception
1619 * when loading a module. This will report any errors encountered before
1720 * ExceptionsManager is configured.
1821 */
19- let _globalHandler = function onError ( e ) {
22+ let _globalHandler : ErrorHandler = function onError (
23+ e : mixed ,
24+ isFatal : boolean ,
25+ ) {
2026 throw e ;
2127} ;
2228
@@ -29,21 +35,31 @@ let _globalHandler = function onError(e) {
2935 * set) globally before requiring anything.
3036 */
3137const ErrorUtils = {
32- setGlobalHandler ( fun ) {
38+ setGlobalHandler ( fun : ErrorHandler ) : void {
3339 _globalHandler = fun ;
3440 } ,
35- getGlobalHandler ( ) {
41+ getGlobalHandler ( ) : ErrorHandler {
3642 return _globalHandler ;
3743 } ,
38- reportError ( error ) {
39- _globalHandler && _globalHandler ( error ) ;
44+ reportError ( error : mixed ) : void {
45+ _globalHandler && _globalHandler ( error , false ) ;
4046 } ,
41- reportFatalError ( error ) {
47+ reportFatalError ( error : mixed ) : void {
48+ // NOTE: This has an untyped call site in Metro.
4249 _globalHandler && _globalHandler ( error , true ) ;
4350 } ,
44- applyWithGuard ( fun , context , args ) {
51+ applyWithGuard < TArgs : $ReadOnlyArray < mixed > , TOut > (
52+ fun : Fn < TArgs , TOut > ,
53+ context ?: ?mixed ,
54+ args ?: ?TArgs ,
55+ // Unused, but some code synced from www sets it to null.
56+ unused_onError ?: null ,
57+ // Some callers pass a name here, which we ignore.
58+ unused_name ?: ?string ,
59+ ) : ?TOut {
4560 try {
4661 _inGuard ++ ;
62+ // $FlowFixMe: TODO T48204745 (1) apply(context, null) is fine. (2) array -> rest array should work
4763 return fun . apply ( context , args ) ;
4864 } catch ( e ) {
4965 ErrorUtils . reportError ( e ) ;
@@ -52,30 +68,41 @@ const ErrorUtils = {
5268 }
5369 return null ;
5470 } ,
55- applyWithGuardIfNeeded ( fun , context , args ) {
71+ applyWithGuardIfNeeded < TArgs : $ReadOnlyArray < mixed > , TOut > (
72+ fun : Fn < TArgs , TOut > ,
73+ context ?: ?mixed ,
74+ args ?: ?TArgs ,
75+ ) : ?TOut {
5676 if ( ErrorUtils . inGuard ( ) ) {
77+ // $FlowFixMe: TODO T48204745 (1) apply(context, null) is fine. (2) array -> rest array should work
5778 return fun . apply ( context , args ) ;
5879 } else {
5980 ErrorUtils . applyWithGuard ( fun , context , args ) ;
6081 }
6182 return null ;
6283 } ,
63- inGuard ( ) {
64- return _inGuard ;
84+ inGuard ( ) : boolean {
85+ return ! ! _inGuard ;
6586 } ,
66- guard ( fun , name , context ) {
87+ guard < TArgs : $ReadOnlyArray < mixed > , TOut > (
88+ fun : Fn < TArgs , TOut > ,
89+ name ?: ?string ,
90+ context ?: ?mixed ,
91+ ) : ?( ...TArgs ) => ?TOut {
92+ // TODO: (moti) T48204753 Make sure this warning is never hit and remove it - types
93+ // should be sufficient.
6794 if ( typeof fun !== 'function' ) {
6895 console . warn ( 'A function must be passed to ErrorUtils.guard, got ' , fun ) ;
6996 return null ;
7097 }
71- name = name || fun . name || '<generated guard>' ;
72- function guarded ( ) {
98+ const guardName = name ?? fun . name ?? '<generated guard>' ;
99+ function guarded ( ... args : TArgs ) : ? TOut {
73100 return ErrorUtils . applyWithGuard (
74101 fun ,
75- context || this ,
76- arguments ,
102+ context ?? this ,
103+ args ,
77104 null ,
78- name ,
105+ guardName ,
79106 ) ;
80107 }
81108
@@ -84,3 +111,5 @@ const ErrorUtils = {
84111} ;
85112
86113global . ErrorUtils = ErrorUtils ;
114+
115+ export type ErrorUtilsT = typeof ErrorUtils ;
0 commit comments