Documentation
Error Handling
SDK error classes and how to handle failures gracefully.
Error Handling
The SDK provides a hierarchy of typed error classes for different failure modes. All errors extend the base AutoBlogWriterError class.
Error Hierarchy
AutoBlogWriterError
├── ConfigError — Invalid configuration
├── ApiError — API request failures
│ └── NotFoundError — 404 responses
AutoBlogWriterError
Base class for all SDK errors.
import { AutoBlogWriterError } from "@autoblogwriter/sdk";
try {
await client.getPosts();
} catch (error) {
if (error instanceof AutoBlogWriterError) {
console.error("SDK error:", error.message);
console.error("Cause:", error.causeError); // Original error, if any
}
}| Property | Type | Description |
|---|---|---|
message | string | Human-readable error message |
causeError | unknown | Original underlying error |
ConfigError
Thrown when the client is created with invalid configuration.
import { createAutoBlogWriterClient, ConfigError } from "@autoblogwriter/sdk";
try {
const client = createAutoBlogWriterClient({
apiUrl: "https://api.autoblogwriter.app",
apiKey: "", // Empty — throws ConfigError
workspaceSlug: "demo",
});
} catch (error) {
if (error instanceof ConfigError) {
console.error(error.message);
// "apiKey is required to authenticate with AutoBlogWriter"
}
}Common Config Errors
| Message | Cause |
|---|---|
apiKey is required to authenticate with AutoBlogWriter | Empty or missing API key |
Provide either workspaceId or workspaceSlug | Neither workspace identifier provided |
apiUrl is required | Missing API URL |
workspaceSlug is required to call the AutoBlogWriter public API | Calling getPosts() without a workspace slug |
ApiError
Thrown when the API returns a non-OK response or a network error occurs.
import { ApiError } from "@autoblogwriter/sdk";
try {
await client.getPosts();
} catch (error) {
if (error instanceof ApiError) {
console.error("Status:", error.status); // HTTP status code (0 for network errors)
console.error("Code:", error.code); // API error code (e.g., "NOT_FOUND")
console.error("Message:", error.message);
console.error("Details:", error.details); // Additional error context
}
}| Property | Type | Description |
|---|---|---|
status | number | HTTP status code. 0 for network failures. 408 for timeouts. |
code | string | undefined | API error code from the response body |
details | unknown | Additional error details from the API |
message | string | Human-readable error message |
Common API Errors
| Status | Scenario | Message |
|---|---|---|
0 | Network failure | Network request to AutoBlogWriter failed: ... |
400 | Invalid slug | slug is required |
401 | Invalid API key | Unauthorized |
404 | Post not found | Not found |
408 | Timeout | Request timed out after 10000ms |
500 | Server error | Varies |
NotFoundError
A convenience subclass of ApiError with a default status of 404.
import { NotFoundError } from "@autoblogwriter/sdk";
try {
// ...
} catch (error) {
if (error instanceof NotFoundError) {
// Render a 404 page
}
}404 Handling
The getPostBySlug method treats 404 responses as a normal result rather than an error:
const post = await client.getPostBySlug("nonexistent-post");
// Returns null — does NOT throwThe fetchBlogPost helper from @autoblogwriter/sdk/next goes further and calls Next.js notFound():
import { fetchBlogPost } from "@autoblogwriter/sdk/next";
const post = await fetchBlogPost("nonexistent-post");
// Automatically calls notFound() — renders nearest not-found.tsxTimeout Handling
The SDK enforces a configurable request timeout (default: 10 seconds):
const client = createAutoBlogWriterClient({
apiUrl: "https://api.autoblogwriter.app",
apiKey: "...",
workspaceSlug: "demo",
timeoutMs: 5000, // 5 seconds
});
try {
await client.getPosts();
} catch (error) {
if (error instanceof ApiError && error.status === 408) {
console.error("Request timed out");
}
}Error Handling Patterns
Server Component (Next.js)
// app/blog/[slug]/page.tsx
import { fetchBlogPost } from "@autoblogwriter/sdk/next";
import { BlogPost } from "@autoblogwriter/sdk/react";
export default async function PostPage({ params }: Props) {
const { slug } = await params;
// fetchBlogPost calls notFound() automatically for 404s
// Other errors will bubble up to error.tsx
const post = await fetchBlogPost(slug);
return <BlogPost post={post} />;
}// app/blog/[slug]/error.tsx
"use client";
export default function Error({ error, reset }: { error: Error; reset: () => void }) {
return (
<div>
<h2>Something went wrong</h2>
<p>{error.message}</p>
<button onClick={reset}>Try again</button>
</div>
);
}Manual Client
import { createAutoBlogWriterClient, ApiError, ConfigError } from "@autoblogwriter/sdk";
const client = createAutoBlogWriterClient({ /* ... */ });
try {
const { posts } = await client.getPosts();
} catch (error) {
if (error instanceof ConfigError) {
// Fix your configuration
} else if (error instanceof ApiError) {
if (error.status === 401) {
// Invalid API key
} else if (error.status === 408) {
// Timeout — retry or increase timeoutMs
} else {
// Other API error
}
}
}Next Steps
- Client Reference — Client configuration and methods.
- Environment Config — Environment variable validation.