@@ -60,13 +60,23 @@ export class DynamicNodeProcessor {
60
60
}
61
61
62
62
// Put it in the next macro task to ensure that the current synchronization script is executed
63
- private dispatchEvent ( type : string ) {
63
+ private dispatchEvent ( type : string , errInfo ?: ErrorEventInit ) {
64
64
setTimeout ( ( ) => {
65
- const event : Event & { garfish ?: boolean } = new Event ( type ) ;
66
- event . garfish = true ;
65
+ const isError = type === 'error' ;
66
+ let event : Event & { __byGarfish__ ?: boolean } ;
67
+
68
+ if ( isError ) {
69
+ event = new ErrorEvent ( type , {
70
+ ...errInfo ,
71
+ message : errInfo . error . message ,
72
+ } ) ;
73
+ } else {
74
+ event = new Event ( type ) ;
75
+ }
76
+ event . __byGarfish__ = true ;
67
77
Object . defineProperty ( event , 'target' , { value : this . el } ) ;
68
78
this . el . dispatchEvent ( event ) ;
69
- type === 'error' && window . dispatchEvent ( event ) ;
79
+ isError && window . dispatchEvent ( event ) ;
70
80
} ) ;
71
81
}
72
82
@@ -88,7 +98,10 @@ export class DynamicNodeProcessor {
88
98
} )
89
99
. catch ( ( e ) => {
90
100
__DEV__ && warn ( e ) ;
91
- this . dispatchEvent ( 'error' ) ;
101
+ this . dispatchEvent ( 'error' , {
102
+ error : e ,
103
+ filename : fetchUrl ,
104
+ } ) ;
92
105
} ) ;
93
106
}
94
107
} else {
@@ -112,12 +125,18 @@ export class DynamicNodeProcessor {
112
125
this . sandbox . loader
113
126
. load < JavaScriptManager > ( namespace , fetchUrl )
114
127
. then ( ( { resourceManager : { url, scriptCode } } ) => {
115
- this . dispatchEvent ( 'load' ) ;
116
- this . sandbox . execScript ( scriptCode , { } , url , { noEntry : true } ) ;
128
+ // It is necessary to ensure that the code execution error cannot trigger the `el.onerror` event
129
+ setTimeout ( ( ) => {
130
+ this . dispatchEvent ( 'load' ) ;
131
+ this . sandbox . execScript ( scriptCode , { } , url , { noEntry : true } ) ;
132
+ } ) ;
117
133
} )
118
134
. catch ( ( e ) => {
119
135
__DEV__ && warn ( e ) ;
120
- this . dispatchEvent ( 'error' ) ;
136
+ this . dispatchEvent ( 'error' , {
137
+ error : e ,
138
+ filename : fetchUrl ,
139
+ } ) ;
121
140
} ) ;
122
141
} else if ( code ) {
123
142
this . sandbox . execScript ( code , { } , baseUrl , { noEntry : true } ) ;
0 commit comments