@@ -92,15 +92,28 @@ export class DataExtractorApiImpl implements DataExtractorApi {
92
92
}
93
93
}
94
94
95
- const data = usedExtraction . extractData ( ) ;
96
- return this . toJson ( {
97
- kind : "Data" ,
98
- extractionResult : {
99
- data,
100
- usedExtractor : mapExtractor ( usedExtraction ) ,
101
- availableExtractors : extractions . map ( mapExtractor ) ,
102
- } ,
103
- } as DataResult ) ;
95
+ try {
96
+ const data = usedExtraction . extractData ( ) ;
97
+
98
+ return this . toJson ( {
99
+ kind : "Data" ,
100
+ extractionResult : {
101
+ data,
102
+ usedExtractor : mapExtractor ( usedExtraction ) ,
103
+ availableExtractors : extractions . map ( mapExtractor ) ,
104
+ } ,
105
+ } as DataResult ) ;
106
+
107
+ } catch ( e ) {
108
+ return this . toJson ( {
109
+ kind : "Data" ,
110
+ extractionResult : {
111
+ data : visualizeError ( e ) ,
112
+ usedExtractor : mapExtractor ( usedExtraction ) ,
113
+ availableExtractors : extractions . map ( mapExtractor ) ,
114
+ } ,
115
+ } as DataResult ) ;
116
+ }
104
117
}
105
118
106
119
public getExtractions (
@@ -111,36 +124,52 @@ export class DataExtractorApiImpl implements DataExtractorApi {
111
124
const extractors = new Array < DataExtractor > ( ) ;
112
125
113
126
for ( const fn of this . extractorSources . values ( ) ) {
114
- fn ( ( extractor ) => {
115
- extractors . push ( extractor ) ;
116
- } , helpers ) ;
127
+ try {
128
+ fn ( ( extractor ) => {
129
+ extractors . push ( extractor ) ;
130
+ } , helpers ) ;
131
+ } catch ( e ) {
132
+ console . error ( 'Error in data extractor source' , fn , e ) ;
133
+ }
117
134
}
118
135
119
- for ( const e of [ ...this . extractors . values ( ) , ...extractors ] ) {
120
- if ( e . dataCtor !== undefined ) {
136
+ for ( const extractor of [ ...this . extractors . values ( ) , ...extractors ] ) {
137
+ if ( extractor . dataCtor !== undefined ) {
121
138
if (
122
139
typeof value !== "object" ||
123
140
value === null ||
124
- value . constructor . name !== e . dataCtor
141
+ value . constructor . name !== extractor . dataCtor
125
142
) {
126
143
continue ;
127
144
}
128
145
}
129
- e . getExtractions (
130
- value ,
131
- {
132
- addExtraction ( extraction ) {
133
- if ( extraction . id === undefined ) {
134
- extraction . id = e . id ;
135
- }
136
- if ( extraction . name === undefined ) {
137
- extraction . name = e . id ;
138
- }
139
- extractions . push ( extraction ) ;
146
+
147
+ try {
148
+ extractor . getExtractions (
149
+ value ,
150
+ {
151
+ addExtraction ( extraction ) {
152
+ if ( extraction . id === undefined ) {
153
+ extraction . id = extractor . id ;
154
+ }
155
+ if ( extraction . name === undefined ) {
156
+ extraction . name = extractor . id ;
157
+ }
158
+ extractions . push ( extraction ) ;
159
+ } ,
140
160
} ,
141
- } ,
142
- context
143
- ) ;
161
+ context
162
+ ) ;
163
+ } catch ( e ) {
164
+ extractions . push ( {
165
+ id : extractor . id ,
166
+ name : extractor . id ,
167
+ priority : 0 ,
168
+ extractData ( ) {
169
+ return visualizeError ( e ) ;
170
+ } ,
171
+ } ) ;
172
+ }
144
173
}
145
174
extractions . sort ( ( a , b ) => b . priority - a . priority ) ;
146
175
@@ -164,6 +193,21 @@ export class DataExtractorApiImpl implements DataExtractorApi {
164
193
}
165
194
}
166
195
196
+ function visualizeError ( e : unknown | Error ) : VisualizationData {
197
+ return helpers . asData ( {
198
+ kind : { text : true } ,
199
+ fileName : 'error.md' ,
200
+ text : `# Error while running data extractor\n\n${ formatErrorStr ( e ) } ` ,
201
+ } ) ;
202
+ }
203
+
204
+ function formatErrorStr ( e : unknown | Error ) : string {
205
+ if ( e instanceof Error ) {
206
+ return `${ e . message } \n\nStack:\n${ e . stack } ` ;
207
+ }
208
+ return "" + e ;
209
+ }
210
+
167
211
function mapExtractor ( e : DataExtraction ) : DataExtractorInfo {
168
212
return {
169
213
id : e . id ! as any ,
@@ -184,7 +228,7 @@ class ContextImpl implements DataExtractorContext {
184
228
| SkippedCallFrames
185
229
) [ ] ,
186
230
private readonly _callFrameRequests : CallFrameRequest [ ]
187
- ) { }
231
+ ) { }
188
232
189
233
addCallFrameRequest ( request : CallFrameRequest ) : void {
190
234
this . _callFrameRequests . push ( request ) ;
0 commit comments