Inkdown
Start writing

Merlin Backend

12 filesยท0 subfolders

Shared Workspace

Merlin Backend
01-Orchestration.md

08-ErrorHandling

Shared from "Merlin Backend" on Inkdown

Error Handling Architecture

Overview

The error handling system provides robust resilience through typed errors, automatic retry logic, circuit breakers, and graceful degradation. It ensures system stability even when external services fail.


Error Hierarchy

Plain text

02-DeepResearch.md
03-Search.md
04-Scraping.md
05-Streaming.md
06-MultiProviderLLM.md
07-MemoryAndContext.md
08-ErrorHandling.md
09-RateLimiting.md
10-TaskQueue.md
11-SecurityAndAuth.md
Orchestration-2nd-draft

Typed Error Base Classes

File: src/server/models/error/error.ts

TypeScript
Client Error (User-Facing)
TypeScript
Server Error (Internal)
TypeScript
Prompt Error (LLM Interaction)
TypeScript
Task Error (Background Jobs)
TypeScript

Error Types & Severity

File: src/server/constantsSchemasAndTypes/global/globalConstants.ts

TypeScript

Retry Architecture

File: src/server/utilities/retry.ts

Core Retry Function
TypeScript

Usage Example:

TypeScript
Retry with Higher-Order Function
TypeScript
Parallel Execution with Settling

All Settled (Don't Throw):

TypeScript

All Settled Throw on Error:

TypeScript

Circuit Breaker Pattern

While not explicit circuit breakers, the system uses fail-fast patterns:

Search Provider Cascade
TypeScript
Scraping Tier Fallback
TypeScript

Orchestrator Error Handling

File: src/server/endpoints/unified/orchestrator/toolOrchestrator.ts

Tool-Level Error Isolation
TypeScript
Invalid Tool Name Recovery
TypeScript

Deep Research Error Resilience

File: src/server/endpoints/unified/features/deepResearch/deepResearch.ts

Step-Level Error Handling
TypeScript

Pattern: Every major operation has try-catch. Log error, use fallback, continue execution.


Axios Error Handling

File: src/server/services/axios.ts

TypeScript

Provider Error Mapping:

TypeScript

Summary

The error handling architecture:

  1. Typed Errors: ClientError, ServerError, PromptError, TaskError
  2. Severity Levels: INFO, WARNING, ERROR, CRITICAL
  3. Automatic Retry: retryAsyncFunction with context
  4. Progressive Enhancement: Retry count drives behavior changes
  5. Settle Patterns: settleAll (continue), settleAllOrThrow (fail)
  6. Circuit Breakers: Provider cascades, tier fallbacks
  7. Tool Isolation: One tool error doesn't crash orchestrator
  8. Deep Research Resilience: Every step wrapped in try-catch
  9. Graceful Degradation: Empty results instead of crashes
  10. Security: Remove sensitive data from error objects

Key Principle: Fail gracefully, never crash the conversation. Log everything, alert on patterns, degrade service rather than deny it.