Skip to content

Commit b0ceba6

Browse files
committed
fix: improve invalid image URLs handling
1 parent 8bad701 commit b0ceba6

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

index.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
8989
}
9090
return {};
9191
} else if (attachmentFiles.length !== 0) {
92+
try {
93+
for (const fileUrl of attachmentFiles) {
94+
new URL(fileUrl);
95+
}
96+
} catch (e) {
97+
jobs.set(jobId, { status: 'failed', error: 'One of the image URLs is not valid' });
98+
return { ok: false, error: 'One of the image URLs is not valid' };
99+
}
92100
//create prompt for OpenAI
93101
const compiledOutputFields = this.compileOutputFieldsTemplates(record);
94102
const prompt = `Analyze the following image(s) and return a single JSON in format like: {'param1': 'value1', 'param2': 'value2'}.
@@ -118,7 +126,13 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
118126
}
119127

120128
//parse response and update record
121-
const resData = JSON.parse(textOutput);
129+
let resData;
130+
try {
131+
resData = JSON.parse(textOutput);
132+
} catch (e) {
133+
jobs.set(jobId, { status: 'failed', error: 'AI response is not valid JSON. Probably attached invalid image URL' });
134+
return { ok: false, error: 'AI response is not valid JSON. Probably attached invalid image URL' };
135+
}
122136
const result = resData;
123137
jobs.set(jobId, { status: 'completed', result });
124138
return { ok: true };
@@ -180,6 +194,14 @@ export default class BulkAiFlowPlugin extends AdminForthPlugin {
180194
attachmentFiles = [];
181195
} else {
182196
attachmentFiles = await this.options.attachFiles({ record });
197+
try {
198+
for (const fileUrl of attachmentFiles) {
199+
new URL(fileUrl);
200+
}
201+
} catch (e) {
202+
jobs.set(jobId, { status: 'failed', error: 'One of the image URLs is not valid' });
203+
return { ok: false, error: 'One of the image URLs is not valid' };
204+
}
183205
}
184206
const fieldTasks = Object.keys(this.options?.generateImages || {}).map(async (key) => {
185207
const prompt = this.compileGenerationFieldTemplates(record)[key];

0 commit comments

Comments
 (0)