Skip to main content

Error Handling

The Respectify API follows standard REST conventions for error handling:

  • HTTP 200: Success. Response body contains the documented schema for that endpoint (e.g., CommentScore, SpamDetectionResult).
  • HTTP 4xx/5xx: Error. Response body contains a standard Error object with error, message, and code fields.

All language SDKs provide typed exceptions for different error conditions—the libraries wrap HTTP REST interaction and surface errors as exceptions. Catch these to handle specific failure modes in your application.

Exception Reference

AuthenticationError (401)

AuthenticationError

Raised when your email or API key is invalid, or the API key has been revoked.

Common causes:

  • Typo in email or API key
  • API key was revoked from the dashboard

BadRequestError (400)

BadRequestError

Raised when the request parameters are invalid.

Common causes:

  • Empty comment text (e.g., passing an empty string to evaluateComment, checkSpam, checkRelevance, etc.)
  • Invalid article ID (malformed UUID, or an ID that doesn't exist)
  • Missing required parameters

PaymentRequiredError (402)

PaymentRequiredError

Raised when your subscription doesn't cover the requested endpoint, or has expired.

Common causes:

  • Subscription has lapsed
  • Trying to use a feature not included in your plan

UnsupportedMediaTypeError (415)

UnsupportedMediaTypeError

Raised when calling initTopicFromUrl and the URL points to a file type that Respectify cannot parse.

Supported formats: HTML pages, Markdown files, PDF documents, plain text files.

Common causes:

  • URL points to an image, video, or binary file
  • URL points to a document type like .docx or .xlsx
  • Server returns an unexpected Content-Type header

What to do: Use initTopicFromText instead, or contact us to request support for the format you need.

ServerError (500)

ServerError

Raised when something goes wrong on the Respectify server.

What to do: Retry after a short delay. If the error persists, contact support with the full error message and request details so we can investigate.

ResponseParseError (TypeScript only)

ResponseParseError

Raised when the API returns invalid JSON or an unexpected response structure.

What to do: This usually indicates a transient server issue. Retry after a short delay.

Error Response Format

All non-200 HTTP responses return a JSON object with this structure:

FieldTypeDescription
errorstringShort error title (e.g., "Unauthorized", "Missing Parameter")
messagestringDetailed error message explaining what went wrong
codeintegerHTTP status code (same as the response status)

Example:

{
"error": "Subscription required",
"message": "An active subscription is required to access this API.",
"code": 402
}

Basic Error Handling

import {
RespectifyClient,
AuthenticationError,
BadRequestError,
PaymentRequiredError,
UnsupportedMediaTypeError,
ServerError,
} from "@respectify/client";

const client = new RespectifyClient({ email, apiKey });

try {
const result = await client.checkSpam("test comment", articleId);
} catch (e) {
if (e instanceof AuthenticationError) {
console.error(`Auth failed: ${e.message}`);
// Check your email and API key
} else if (e instanceof BadRequestError) {
console.error(`Bad request: ${e.message}`);
// Check comment text isn't empty, articleId is valid
} else if (e instanceof PaymentRequiredError) {
console.error(`Payment needed: ${e.message}`);
// Check your subscription status
} else if (e instanceof UnsupportedMediaTypeError) {
console.error(`Unsupported format: ${e.message}`);
// Only raised by initTopicFromUrl
} else if (e instanceof ServerError) {
console.error(`Server error: ${e.message}`);
// Retry after a delay
}
}

Retry Strategies

For transient errors (5xx), use exponential backoff. We recommend using established libraries rather than rolling your own:

Use a simple retry wrapper or a library like p-retry:

import { ServerError } from "@respectify/client";

async function withRetry<T>(
fn: () => Promise<T>,
maxRetries = 3
): Promise<T> {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
return await fn();
} catch (e) {
if (e instanceof ServerError && attempt < maxRetries) {
await new Promise((r) => setTimeout(r, 2 ** (attempt - 1) * 1000));
continue;
}
throw e;
}
}
throw new Error("Unreachable");
}

const result = await withRetry(() =>
client.checkSpam(comment, articleId)
);