Skip to content

Commit 6b41307

Browse files
committed
handle errors in runs
1 parent 0f68e38 commit 6b41307

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/tools/run.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import zodToJsonSchema from 'zod-to-json-schema';
55
import { ApifyClient } from '../apify-client.js';
66
import { HelperTools } from '../const.js';
77
import type { InternalTool, ToolEntry } from '../types.js';
8+
import { ApifyApiError } from 'apify-client';
9+
import log from '@apify/log';
810

911
const ajv = new Ajv({ coerceTypes: 'array', strict: false });
1012

@@ -38,6 +40,9 @@ export const getActorRun: ToolEntry = {
3840
}
3941
const client = new ApifyClient({ token: apifyToken });
4042
const v = await client.run(parsed.runId).get();
43+
if (!v) {
44+
return { content: [{ type: 'text', text: `Run with ID '${parsed.runId}' not found.` }] };
45+
}
4146
return { content: [{ type: 'text', text: JSON.stringify(v) }] };
4247
},
4348
} as InternalTool,
@@ -99,8 +104,16 @@ export const abortActorRun: ToolEntry = {
99104
return { content: [{ type: 'text', text: 'Run ID is required.' }] };
100105
}
101106
const client = new ApifyClient({ token: apifyToken });
102-
const v = await client.run(parsed.runId).abort({ gracefully: parsed.gracefully });
103-
return { content: [{ type: 'text', text: JSON.stringify(v) }] };
107+
try {
108+
const v = await client.run(parsed.runId).abort({ gracefully: parsed.gracefully });
109+
return { content: [{ type: 'text', text: JSON.stringify(v) }] };
110+
} catch (error) {
111+
if (error instanceof ApifyApiError) {
112+
log.error(`[abortActorRun] Failed to abort run ${parsed.runId}: ${error.message}`);
113+
return { content: [{ type: 'text', text: `Failed to abort run: ${error.message}` }] };
114+
}
115+
throw error;
116+
}
104117
},
105118
} as InternalTool,
106119
};

src/tools/run_collection.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Ajv } from 'ajv';
2+
import { ApifyApiError } from 'apify-client';
23
import { z } from 'zod';
34
import zodToJsonSchema from 'zod-to-json-schema';
45

@@ -41,8 +42,15 @@ export const getUserRunsList: ToolEntry = {
4142
const { args, apifyToken } = toolArgs;
4243
const parsed = getUserRunsListArgs.parse(args);
4344
const client = new ApifyClient({ token: apifyToken });
44-
const runs = await client.runs().list({ limit: parsed.limit, offset: parsed.offset, desc: parsed.desc, status: parsed.status });
45-
return { content: [{ type: 'text', text: JSON.stringify(runs) }] };
45+
try {
46+
const runs = await client.runs().list({ limit: parsed.limit, offset: parsed.offset, desc: parsed.desc, status: parsed.status });
47+
return { content: [{ type: 'text', text: JSON.stringify(runs) }] };
48+
} catch (error) {
49+
if (error instanceof ApifyApiError) {
50+
return { content: [{ type: 'text', text: `Failed to get runs list: ${error.message}` }] };
51+
}
52+
throw error;
53+
}
4654
},
4755
} as InternalTool,
4856
};

0 commit comments

Comments
 (0)