Skip to content

Implement structured error logging with error codes #17

@BunsDev

Description

@BunsDev

Problem

Error handling across the codebase is inconsistent:

  • Some try-catch blocks silently swallow errors (especially JSON parsing in ingestion)
  • API routes return generic "Unexpected server error" with no error code
  • No structured logging format — errors are logged as raw strings or not at all
  • Debugging production issues requires reading source code to trace error origins

Proposed Solution

1. Error Code Registry

// lib/opentrust/errors.ts
export enum ErrorCode {
  DB_CONNECTION_FAILED = "DB_CONNECTION_FAILED",
  DB_QUERY_FAILED = "DB_QUERY_FAILED",
  AUTH_INVALID_CREDENTIALS = "AUTH_INVALID_CREDENTIALS",
  AUTH_TOKEN_EXPIRED = "AUTH_TOKEN_EXPIRED",
  VALIDATION_FAILED = "VALIDATION_FAILED",
  INGESTION_PARSE_ERROR = "INGESTION_PARSE_ERROR",
  MEMORY_NOT_FOUND = "MEMORY_NOT_FOUND",
  SEARCH_QUERY_INVALID = "SEARCH_QUERY_INVALID",
  // ...
}

2. Structured Logger

// lib/opentrust/logger.ts
logger.error({
  code: ErrorCode.INGESTION_PARSE_ERROR,
  message: "Failed to parse session JSON",
  context: { sessionId, filePath },
  error: originalError,
});

3. Audit Trail

  • Errors logged with timestamp, code, context, and stack trace
  • Replace all silent catch blocks with explicit logging

Acceptance Criteria

  • Error code enum covers all known error categories
  • Structured logger utility created with error, warn, info levels
  • All try-catch blocks use the structured logger (no silent swallowing)
  • API error responses include error codes
  • Unit tests for the logger utility

🤖 Generated with Claude Code

Metadata

Metadata

Assignees

No one assigned

    Labels

    dxDeveloper experienceenhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions