try {
const farm = await callApi(url, init);
// ...
} catch (error) {
if (!(error instanceof WiseyieldApiError)) throw error;
switch (error.code) {
case 'VALIDATION_ERROR':
// error.details is { fieldName: [messages] }
return renderFieldErrors(error.details);
case 'INSUFFICIENT_SCOPE':
// error.details.requiredScope tells you which scope to add
return promptKeyRotation(error.details.requiredScope);
case 'RATE_LIMIT_EXCEEDED':
// Use X-RateLimit-Reset to schedule the retry
return scheduleRetry();
case 'NOT_FOUND':
return null; // Treat as missing rather than failure
case 'CONFLICT':
// Idempotent create — fetch the existing resource
return fetchExisting();
default:
// Unknown error — log and propagate
logger.error('Unhandled WiseYield API error', error);
throw error;
}
}