Skip to content

Error Handling

This guide explains the error codes returned by the Koral API and how to handle them in your applications.

When an error occurs, the Koral API returns a JSON response with an error object:

{
"error": {
"code": "invalid_search_id",
"message": "The Search id provided is invalid or has expired.",
"documentation_url": "https://docs.koral.ai/errors#invalid_search_id"
}
}
CodeResponse codeDescription
invalid_search_id401The search id is invalid or has expired
missing_earch_id401No search id was provided
CodeResponse codeDescription
invalid_request400The request was malformed or missing required parameters
invalid_parameter400One or more parameters have an invalid value
unsupported_image_format400The image format is not supported
image_too_large400The image exceeds the maximum allowed size
CodeResponse codeDescription
rate_limit_exceeded429You’ve exceeded your rate limit
quota_exceeded429You’ve exceeded your monthly quota
CodeResponse codeDescription
internal_error500An unexpected error occurred on the server
service_unavailable503The service is temporarily unavailable

Here’s an example of how to handle errors when using the Koral API:

async function searchImages(query) {
try {
const response = await fetch('https://api.koral.ai/v1/search', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
query: query
})
});
const data = await response.json();
if (!response.ok) {
const statusCode = response.status;
// Handle API error
const error = data.error;
console.error(`Error ${error.statusCode}: ${error.message}`);
// Handle specific error types
if (error.code === 'rate_limit_exceeded') {
// Wait and retry later
return scheduleRetry(query);
}
throw new Error(error.message);
}
return data;
} catch (error) {
// Handle network or parsing errors
console.error('Request failed:', error);
throw error;
}
}

For rate limiting errors (429), we recommend implementing an exponential backoff strategy:

function scheduleRetry(query, attempt = 1) {
const maxAttempts = 5;
const baseDelay = 1000; // 1 second
if (attempt > maxAttempts) {
throw new Error('Maximum retry attempts exceeded');
}
const delay = baseDelay * Math.pow(2, attempt - 1);
return new Promise((resolve) => {
setTimeout(() => {
resolve(searchImages(query, attempt + 1));
}, delay);
});
}