Skip to content

Commit 84762b2

Browse files
committed
Implements error handling for JS
1 parent a9656e1 commit 84762b2

File tree

1 file changed

+74
-30
lines changed

1 file changed

+74
-30
lines changed

data-extraction/src/js/api/DataExtractorApiImpl.ts

Lines changed: 74 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,28 @@ export class DataExtractorApiImpl implements DataExtractorApi {
9292
}
9393
}
9494

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+
}
104117
}
105118

106119
public getExtractions(
@@ -111,36 +124,52 @@ export class DataExtractorApiImpl implements DataExtractorApi {
111124
const extractors = new Array<DataExtractor>();
112125

113126
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+
}
117134
}
118135

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) {
121138
if (
122139
typeof value !== "object" ||
123140
value === null ||
124-
value.constructor.name !== e.dataCtor
141+
value.constructor.name !== extractor.dataCtor
125142
) {
126143
continue;
127144
}
128145
}
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+
},
140160
},
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+
}
144173
}
145174
extractions.sort((a, b) => b.priority - a.priority);
146175

@@ -164,6 +193,21 @@ export class DataExtractorApiImpl implements DataExtractorApi {
164193
}
165194
}
166195

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+
167211
function mapExtractor(e: DataExtraction): DataExtractorInfo {
168212
return {
169213
id: e.id! as any,
@@ -184,7 +228,7 @@ class ContextImpl implements DataExtractorContext {
184228
| SkippedCallFrames
185229
)[],
186230
private readonly _callFrameRequests: CallFrameRequest[]
187-
) {}
231+
) { }
188232

189233
addCallFrameRequest(request: CallFrameRequest): void {
190234
this._callFrameRequests.push(request);

0 commit comments

Comments
 (0)