molroo docs
SDK Reference

Errors

MolrooApiError and WorldApiError classes and error codes.

Errors

The SDK provides two error classes — MolrooApiError for persona operations and WorldApiError for world operations. Both include a machine-readable code and HTTP status for programmatic handling.

MolrooApiError

Thrown by MolrooPersona methods and persona-related operations.

import { MolrooApiError } from '@molroo-io/sdk';
class MolrooApiError extends Error {
  message: string;
  code: string;
  status: number;
}

The name property is always 'MolrooApiError'.

Error Codes

CodeStatusDescription
UNAUTHORIZED401Invalid or missing API key
RATE_LIMITED429Too many requests
WORLD_NOT_FOUND404World ID does not exist
ENTITY_NOT_FOUND404Entity name/ID not found in the world
VALIDATION_ERROR400Invalid request body (missing fields, wrong types)
MUTATE_FAILED400Mutation operation failed
LLM_NOT_CONFIGURED400LLM adapter required but not provided
LLM_REQUIRED400LLM adapter required when using description string for persona creation
UNKNOWNvariesUnclassified server error

Example

import { MolrooApiError } from '@molroo-io/sdk';

try {
  await persona.chat('Hello');
} catch (e) {
  if (e instanceof MolrooApiError) {
    switch (e.code) {
      case 'UNAUTHORIZED':
        console.error('Check your API key');
        break;
      case 'RATE_LIMITED':
        console.error('Too many requests, retrying...');
        break;
      case 'LLM_NOT_CONFIGURED':
        console.error('Provide an LLM adapter or pass appraisal manually');
        break;
      default:
        console.error(`API error [${e.code}]: ${e.message}`);
    }
  }
}

WorldApiError

Thrown by World methods and world-related operations.

import { WorldApiError } from '@molroo-io/sdk/world';
class WorldApiError extends Error {
  message: string;
  code: string;
  status: number;
}

The name property is always 'WorldApiError'.

Error Codes

CodeStatusDescription
UNAUTHORIZED401Invalid or missing API key
WORLD_NOT_FOUND404World ID does not exist or belongs to another tenant
WORLD_PERSONA_NOT_FOUND404Persona not found in the world
WORLD_PERSONA_ALREADY_EXISTS409Persona is already a member of the world
ACTION_NOT_FOUND404Action name not found (default or custom)
RELATIONSHIP_NOT_FOUND404Relationship ID not found
VALIDATION_ERROR400Invalid request body (missing fields, wrong types)
UNKNOWNvariesUnclassified server error

Example

import { Molroo } from '@molroo-io/sdk';
import { WorldApiError } from '@molroo-io/sdk/world';

const molroo = new Molroo({ apiKey: 'mk_live_...' });

try {
  const world = await molroo.getWorld('nonexistent-id');
} catch (e) {
  if (e instanceof WorldApiError) {
    switch (e.code) {
      case 'WORLD_NOT_FOUND':
        console.error('World does not exist');
        break;
      case 'WORLD_PERSONA_NOT_FOUND':
        console.error('Add the persona to the world first');
        break;
      default:
        console.error(`API error [${e.code}]: ${e.message}`);
    }
  }
}

API Response Parsing

The SDK automatically parses API responses and unwraps the { result: T } envelope. When the API returns a non-OK HTTP status, the error response is parsed and thrown as the appropriate error class:

// API error response format:
{
  "error": {
    "code": "ENTITY_NOT_FOUND",
    "message": "Entity \"NonExistent\" not found in world"
  }
}

// Becomes:
MolrooApiError {
  name: 'MolrooApiError',
  message: 'Entity "NonExistent" not found in world',
  code: 'ENTITY_NOT_FOUND',
  status: 404,
}

On this page