Docs
Errors
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
  }
}
PropertyTypeDescription
messagestringHuman-readable error message
causeErrorunknownOriginal 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

MessageCause
apiKey is required to authenticate with AutoBlogWriterEmpty or missing API key
Provide either workspaceId or workspaceSlugNeither workspace identifier provided
apiUrl is requiredMissing API URL
workspaceSlug is required to call the AutoBlogWriter public APICalling 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
  }
}
PropertyTypeDescription
statusnumberHTTP status code. 0 for network failures. 408 for timeouts.
codestring | undefinedAPI error code from the response body
detailsunknownAdditional error details from the API
messagestringHuman-readable error message

Common API Errors

StatusScenarioMessage
0Network failureNetwork request to AutoBlogWriter failed: ...
400Invalid slugslug is required
401Invalid API keyUnauthorized
404Post not foundNot found
408TimeoutRequest timed out after 10000ms
500Server errorVaries

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 throw

The 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.tsx

Timeout 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