Error Handling
The Mastra Client SDK includes built-in retry mechanism and error handling capabilities.
Error Handling
All API methods can throw errors that you can catch and handle:
try {
const agent = client.getAgent("agent-id");
const response = await agent.generate({
messages: [{ role: "user", content: "Hello" }],
});
} catch (error) {
console.error("An error occurred:", error.message);
}
Retry Mechanism
The client automatically retries failed requests with exponential backoff:
const client = new MastraClient({
baseUrl: "http://localhost:4111",
retries: 3, // Number of retry attempts
backoffMs: 300, // Initial backoff time
maxBackoffMs: 5000, // Maximum backoff time
});
How Retries Work
- First attempt fails → Wait 300ms
- Second attempt fails → Wait 600ms
- Third attempt fails → Wait 1200ms
- Final attempt fails → Throw error