Skip to content

Commit 3fec5c0

Browse files
committed
fix: simplify code by removing nested try catch
1 parent e127971 commit 3fec5c0

File tree

1 file changed

+33
-52
lines changed

1 file changed

+33
-52
lines changed

src/tools/dataset.ts

Lines changed: 33 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -178,66 +178,47 @@ export const getDatasetSchema: ToolEntry = {
178178
const parsed = getDatasetSchemaArgs.parse(args);
179179
const client = new ApifyClient({ token: apifyToken });
180180

181+
// Get dataset items
182+
const datasetResponse = await client.dataset(parsed.datasetId).listItems({
183+
clean: parsed.clean,
184+
limit: parsed.limit,
185+
});
186+
187+
if (!datasetResponse) {
188+
return { content: [{ type: 'text', text: `Dataset '${parsed.datasetId}' not found.` }] };
189+
}
190+
191+
const datasetItems = datasetResponse.items;
192+
193+
if (datasetItems.length === 0) {
194+
return { content: [{ type: 'text', text: `Dataset '${parsed.datasetId}' is empty.` }] };
195+
}
196+
197+
// Clean the dataset items by removing empty arrays
198+
const cleanedDatasetItems = datasetItems.map((item) => removeEmptyArrays(item));
199+
200+
// Try to generate schema with full options first
181201
try {
182-
// Get dataset items
183-
const datasetResponse = await client.dataset(parsed.datasetId).listItems({
184-
clean: parsed.clean,
185-
limit: parsed.limit,
202+
const schema = toJsonSchema(cleanedDatasetItems, {
203+
arrays: { mode: parsed.arrayMode },
204+
objects: { additionalProperties: parsed.additionalProperties },
186205
});
187206

188-
if (!datasetResponse) {
189-
return { content: [{ type: 'text', text: `Dataset '${parsed.datasetId}' not found.` }] };
190-
}
191-
192-
const datasetItems = datasetResponse.items;
193-
194-
if (datasetItems.length === 0) {
195-
return { content: [{ type: 'text', text: `Dataset '${parsed.datasetId}' is empty.` }] };
196-
}
197-
198-
// Clean the dataset items by removing empty arrays
199-
const cleanedDatasetItems = datasetItems.map((item) => removeEmptyArrays(item));
200-
201-
try {
202-
// Generate schema with options to handle arrays better
203-
const schema = toJsonSchema(cleanedDatasetItems, {
204-
arrays: { mode: parsed.arrayMode },
205-
objects: { additionalProperties: parsed.additionalProperties },
206-
// strings: { detectFormat: false },
207-
});
208-
209-
return {
210-
content: [{
211-
type: 'text',
212-
text: JSON.stringify(schema),
213-
}],
214-
};
215-
} catch (schemaError) {
216-
// Fallback: try with a simpler approach
217-
try {
218-
const fallbackSchema = toJsonSchema(cleanedDatasetItems, {
219-
arrays: { mode: 'first' },
220-
});
221-
222-
return {
223-
content: [{ type: 'text', text: JSON.stringify(fallbackSchema) }],
224-
};
225-
} catch (fallbackError) {
226-
return {
227-
content: [{
228-
type: 'text',
229-
text: `Error generating schema: ${(schemaError as Error).message}. Fallback also failed: ${(fallbackError as Error).message}`,
230-
}],
231-
};
232-
}
233-
}
234-
} catch (error) {
235207
return {
236208
content: [{
237209
type: 'text',
238-
text: `Error generating schema: ${(error as Error).message}`,
210+
text: JSON.stringify(schema),
239211
}],
240212
};
213+
} catch {
214+
// Fallback: try with simpler approach
215+
const fallbackSchema = toJsonSchema(cleanedDatasetItems, {
216+
arrays: { mode: 'first' },
217+
});
218+
219+
return {
220+
content: [{ type: 'text', text: JSON.stringify(fallbackSchema) }],
221+
};
241222
}
242223
},
243224
} as InternalTool,

0 commit comments

Comments
 (0)