@@ -47,9 +47,9 @@ export type CallActorGetDatasetResult = {
4747 * If the `APIFY_IS_AT_HOME` the dataset items are pushed to the Apify dataset.
4848 *
4949 * @param {string } actorName - The name of the Actor to call.
50- * @param {ActorCallOptions } callOptions - The options to pass to the Actor.
5150 * @param {unknown } input - The input to pass to the actor.
52- * @param {string } apifyToken - The Apify token to use for authentication.
51+ * @param {ApifyClient } apifyClient - The Apify client to use for authentication.
52+ * @param {ActorCallOptions } callOptions - The options to pass to the Actor.
5353 * @param {ProgressTracker } progressTracker - Optional progress tracker for real-time updates.
5454 * @param {AbortSignal } abortSignal - Optional abort signal to cancel the actor run.
5555 * @returns {Promise<CallActorGetDatasetResult | null> } - A promise that resolves to an object containing the actor run and dataset items.
@@ -64,77 +64,71 @@ export async function callActorGetDataset(
6464 abortSignal ?: AbortSignal ,
6565) : Promise < CallActorGetDatasetResult | null > {
6666 const CLIENT_ABORT = Symbol ( 'CLIENT_ABORT' ) ; // Just internal symbol to identify client abort
67- // TODO: we should remove this throw, we are just catching and then rethrowing with generic message
68- try {
69- const actorClient = apifyClient . actor ( actorName ) ;
67+ const actorClient = apifyClient . actor ( actorName ) ;
7068
71- // Start the actor run
72- const actorRun : ActorRun = await actorClient . start ( input , callOptions ) ;
69+ // Start the actor run
70+ const actorRun : ActorRun = await actorClient . start ( input , callOptions ) ;
7371
74- // Start progress tracking if tracker is provided
75- if ( progressTracker ) {
76- progressTracker . startActorRunUpdates ( actorRun . id , apifyClient , actorName ) ;
77- }
78-
79- // Create abort promise that handles both API abort and race rejection
80- const abortPromise = async ( ) => new Promise < typeof CLIENT_ABORT > ( ( resolve ) => {
81- abortSignal ?. addEventListener ( 'abort' , async ( ) => {
82- // Abort the actor run via API
83- try {
84- await apifyClient . run ( actorRun . id ) . abort ( { gracefully : false } ) ;
85- } catch ( e ) {
86- log . error ( 'Error aborting Actor run' , { error : e , runId : actorRun . id } ) ;
87- }
88- // Reject to stop waiting
89- resolve ( CLIENT_ABORT ) ;
90- } , { once : true } ) ;
91- } ) ;
72+ // Start progress tracking if tracker is provided
73+ if ( progressTracker ) {
74+ progressTracker . startActorRunUpdates ( actorRun . id , apifyClient , actorName ) ;
75+ }
9276
93- // Wait for completion or cancellation
94- const potentialAbortedRun = await Promise . race ( [
95- apifyClient . run ( actorRun . id ) . waitForFinish ( ) ,
96- ...( abortSignal ? [ abortPromise ( ) ] : [ ] ) ,
97- ] ) ;
77+ // Create abort promise that handles both API abort and race rejection
78+ const abortPromise = async ( ) => new Promise < typeof CLIENT_ABORT > ( ( resolve ) => {
79+ abortSignal ?. addEventListener ( 'abort' , async ( ) => {
80+ // Abort the actor run via API
81+ try {
82+ await apifyClient . run ( actorRun . id ) . abort ( { gracefully : false } ) ;
83+ } catch ( e ) {
84+ log . error ( 'Error aborting Actor run' , { error : e , runId : actorRun . id } ) ;
85+ }
86+ // Reject to stop waiting
87+ resolve ( CLIENT_ABORT ) ;
88+ } , { once : true } ) ;
89+ } ) ;
90+
91+ // Wait for completion or cancellation
92+ const potentialAbortedRun = await Promise . race ( [
93+ apifyClient . run ( actorRun . id ) . waitForFinish ( ) ,
94+ ...( abortSignal ? [ abortPromise ( ) ] : [ ] ) ,
95+ ] ) ;
9896
99- if ( potentialAbortedRun === CLIENT_ABORT ) {
100- log . info ( 'Actor run aborted by client' , { actorName, input } ) ;
101- return null ;
102- }
103- const completedRun = potentialAbortedRun as ActorRun ;
104-
105- // Process the completed run
106- const dataset = apifyClient . dataset ( completedRun . defaultDatasetId ) ;
107- const [ datasetItems , defaultBuild ] = await Promise . all ( [
108- dataset . listItems ( ) ,
109- ( await actorClient . defaultBuild ( ) ) . get ( ) ,
110- ] ) ;
111-
112- // Generate schema using the shared utility
113- const generatedSchema = generateSchemaFromItems ( datasetItems . items , {
114- clean : true ,
115- arrayMode : 'all' ,
116- } ) ;
117- const schema = generatedSchema || { type : 'object' , properties : { } } ;
118-
119- /**
120- * Get important fields that are using in any dataset view as they MAY be used in filtering to ensure the output fits
121- * the tool output limits. Client has to use the get-actor-output tool to retrieve the full dataset or filtered out fields.
122- */
123- const storageDefinition = defaultBuild ?. actorDefinition ?. storages ?. dataset as ActorDefinitionStorage | undefined ;
124- const importantProperties = getActorDefinitionStorageFieldNames ( storageDefinition || { } ) ;
125- const previewItems = ensureOutputWithinCharLimit ( datasetItems . items , importantProperties , TOOL_MAX_OUTPUT_CHARS ) ;
126-
127- return {
128- runId : actorRun . id ,
129- datasetId : completedRun . defaultDatasetId ,
130- itemCount : datasetItems . count ,
131- schema,
132- previewItems,
133- } ;
134- } catch ( error ) {
135- log . error ( 'Error calling Actor' , { error, actorName, input } ) ;
136- throw new Error ( `Error calling Actor: ${ error } ` ) ;
97+ if ( potentialAbortedRun === CLIENT_ABORT ) {
98+ log . info ( 'Actor run aborted by client' , { actorName, input } ) ;
99+ return null ;
137100 }
101+ const completedRun = potentialAbortedRun as ActorRun ;
102+
103+ // Process the completed run
104+ const dataset = apifyClient . dataset ( completedRun . defaultDatasetId ) ;
105+ const [ datasetItems , defaultBuild ] = await Promise . all ( [
106+ dataset . listItems ( ) ,
107+ ( await actorClient . defaultBuild ( ) ) . get ( ) ,
108+ ] ) ;
109+
110+ // Generate schema using the shared utility
111+ const generatedSchema = generateSchemaFromItems ( datasetItems . items , {
112+ clean : true ,
113+ arrayMode : 'all' ,
114+ } ) ;
115+ const schema = generatedSchema || { type : 'object' , properties : { } } ;
116+
117+ /**
118+ * Get important fields that are using in any dataset view as they MAY be used in filtering to ensure the output fits
119+ * the tool output limits. Client has to use the get-actor-output tool to retrieve the full dataset or filtered out fields.
120+ */
121+ const storageDefinition = defaultBuild ?. actorDefinition ?. storages ?. dataset as ActorDefinitionStorage | undefined ;
122+ const importantProperties = getActorDefinitionStorageFieldNames ( storageDefinition || { } ) ;
123+ const previewItems = ensureOutputWithinCharLimit ( datasetItems . items , importantProperties , TOOL_MAX_OUTPUT_CHARS ) ;
124+
125+ return {
126+ runId : actorRun . id ,
127+ datasetId : completedRun . defaultDatasetId ,
128+ itemCount : datasetItems . count ,
129+ schema,
130+ previewItems,
131+ } ;
138132}
139133
140134/**
0 commit comments