search endpoint, use an exception handler to call the endpoint.
The client is constructed outside the exception handler, so a missing
SELTZ_API_KEY raises SeltzConfigurationError at startup rather than per-query.from seltz import (
Seltz,
SeltzAuthenticationError,
SeltzConnectionError,
SeltzTimeoutError,
SeltzRateLimitError,
SeltzAPIError,
)
client = Seltz()
def safe_search(query):
try:
return client.search(query)
except SeltzAuthenticationError:
print("Error: Invalid API key")
return None
except SeltzConnectionError:
print("Error: Could not connect to Seltz API")
return None
except SeltzTimeoutError:
print("Error: Request timed out")
return None
except SeltzRateLimitError:
print("Error: Rate limit exceeded, try again later")
return None
except SeltzAPIError as e:
print(f"API Error: {e.grpc_code} - {e.grpc_details}")
return None
response = safe_search("My query")
if response:
print(f"Found {len(response.documents)} documents")
import {
Seltz,
SeltzAuthenticationError,
SeltzConnectionError,
SeltzTimeoutError,
SeltzRateLimitError,
SeltzAPIError,
} from "seltz";
const client = new Seltz();
async function searchWithErrorHandling(query: string) {
try {
return await client.search({ query });
} catch (error) {
if (error instanceof SeltzAuthenticationError) {
console.error('Invalid API key - check your credentials');
throw error;
}
if (error instanceof SeltzConnectionError) {
console.error('Connection failed - check your network');
throw error;
}
if (error instanceof SeltzTimeoutError) {
console.error('Request timed out - try again');
throw error;
}
if (error instanceof SeltzRateLimitError) {
console.error('Rate limited - wait before retrying');
throw error;
}
if (error instanceof SeltzAPIError) {
console.error(`API error: ${error.code} - ${error.message}`);
throw error;
}
throw error;
}
}
response=$(curl -s -w '\n%{http_code}' -X POST https://api.seltz.ai/v1/search \
-H "x-api-key: $SELTZ_API_KEY" \
-H "Content-Type: application/json" \
-d '{"query": "My query"}')
status=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')
case "$status" in
200)
count=$(echo "$body" | jq '.documents | length')
echo "Found $count documents"
;;
401)
echo "Error: Invalid API key"
;;
429)
echo "Error: Rate limit exceeded, try again later"
;;
400)
echo "Error: $body"
;;
500)
echo "Error: Internal server error"
;;
*)
echo "Unexpected status: $status"
;;
esac