Error Handling
When building applications, it's important to anticipate and handle errors. The Inceptron API uses conventional HTTP status codes to indicate the success or failure of an API request.
This guide will show you how to catch and inspect errors.
HTTP Status Codes
Here are some of the common errors you might encounter:
400 Bad Request: Your request was malformed. This could be due to missing required parameters or invalid JSON.401 Unauthorized: Your API key is missing, invalid, or disabled.429 Too Many Requests: You have exceeded your rate limit.500 Internal Server Error: Something went wrong on our end. Please wait and try again.
Handling Errors in Python
The openai Python library raises exceptions for API errors. You can use a try...except block to handle them.
import os
from openai import OpenAI, APIError
client = OpenAI(
base_url="https://api.inceptron.io/v1",
api_key="invalid_key", # Using an invalid key to trigger an error
)
try:
completion = client.chat.completions.create(
model="meta-llama/Llama-3.3-70B-Instruct",
messages=[{"role": "user", "content": "Hello!"}],
)
except APIError as e:
print(f"An API error occurred: {e}")
print(f"Status code: {e.status_code}")
print(f"Response: {e.response.text}")
Handling Errors in JavaScript
The openai JavaScript library also rejects the promise with an error object. You can use a try...catch block with async/await.
import { OpenAI } from "openai";
const client = new OpenAI({
baseURL: "https://api.inceptron.io/v1",
apiKey: "invalid_key", // Using an invalid key
});
async function main() {
try {
const chatCompletion = await client.chat.completions.create({
model: "meta-llama/Llama-3.3-70B-Instruct",
messages: [{ role: "user", content: "Hello!" }],
});
} catch (e) {
if (e instanceof OpenAI.APIError) {
console.error(`An API error occurred: ${e}`);
console.error(`Status code: ${e.status}`);
console.error(`Response headers: ${JSON.stringify(e.headers)}`);
} else {
console.error(`An unexpected error occurred: ${e}`);
}
}
}
main();