Skip to main content
Errors can occur for a number of different reasons, from timeouts and tool failures to network issues and validation errors. The Agent Platform Javascript SDK (AgP JS SDK) provides two easy ways of handling errors:
  • Post-run error handling: Catch errors after a task has finished executing.
  • Real-time error handling: Listens for errors as they happen.

Handle errors after a run

You can catch errors once a task finishes by wrapping execution in a try/catch block.

Wrap with try/catch

Wrap your task in a try/catch block to catch any errors thrown when the task completes.
try {
  const task = await agent.run('Find unicorns on Mars');
  await task.waitForCompletion();
} catch (error) {
  console.error('Task error:', error);
}

Handle specific errors

You can branch on the error message to handle different cases:
try {
  const task = await agent.run('Find unicorns on Mars');
  await task.waitForCompletion();
} catch (error) {
  if (error.message.includes('timeout')) {
    console.log('Task timed out');
  } else if (error.message.includes('failed')) {
    console.log('Task failed:', error);
  } else {
    console.log('Unknown error:', error);
  }
}

Handle errors in real-time

Errors may sometimes occur while a task is running. The AgP JS SDK enables you to listen for them using the onError event.

Attach a real-time error listener

Use onError to be notified of errors during task execution.
task.onError((error) => {
  console.error('Real-time error:', error);
});

Handle the error

Log or forward the error as and when you’re notified of it.
task.onError((error) => {
  console.error('Real-time error:', error);
  notifyErrorService(error); // Send to your monitoring system
});
I