const client = new NodeStash({ apiKey: 'nds_live_...' })try { const contact = await client.contacts.create({ email: 'jane@acme.com', first_name: 'Jane', })} catch (error) { if (error instanceof ValidationError) { // Field-level validation errors console.log('Validation failed:', error.message) for (const detail of error.details) { console.log(` ${detail.field}: ${detail.message}`) } } else if (error instanceof AuthenticationError) { // Invalid or expired API key console.log('Authentication failed:', error.message) } else if (error instanceof NotFoundError) { // Resource doesn't exist console.log('Not found:', error.message) } else if (error instanceof RateLimitError) { // All retries exhausted console.log(`Rate limited. Retry after ${error.retryAfter}s`) } else if (error instanceof ApiError) { // Catch-all for other API errors console.log(`API error ${error.statusCode}: ${error.code}`) } else if (error instanceof NodeStashError) { // Network errors, timeouts console.log('SDK error:', error.message) }}
Always catch specific error classes before the parent class. instanceof checks follow the class hierarchy — ValidationError also matches ApiError and NodeStashError.