feat: add type-safe error handling utilities

- Add isError() type guard to check if value is Error instance
- Add getErrorMessage() utility for safe error message extraction
  in catch blocks (handles TypeScript 4.4+ unknown error type)
- Replace all (err as Error).message patterns with getErrorMessage(err)
  across server.ts, cli.ts, ralph-loop.ts, and screen-manager.ts
- Follows TypeScript best practice of treating caught errors as unknown

This improves code safety by properly handling the case where caught
values may not be Error instances (e.g., thrown strings or objects).

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
arkon
2026-01-21 14:07:04 +01:00
co-authored by Claude Opus 4.5
parent c33d6e0987
commit 384350f731
5 changed files with 74 additions and 32 deletions
+3 -3
View File
@@ -19,7 +19,7 @@ import { getTaskQueue, TaskQueue } from './task-queue.js';
import { getStore, StateStore } from './state-store.js';
import { Session } from './session.js';
import { Task } from './task.js';
import { RalphLoopStatus } from './types.js';
import { RalphLoopStatus, getErrorMessage } from './types.js';
/**
* Events emitted by RalphLoop
@@ -304,10 +304,10 @@ export class RalphLoop extends EventEmitter {
this.emit('taskAssigned', task.id, session.id);
} catch (err) {
task.fail((err as Error).message);
task.fail(getErrorMessage(err));
session.clearTask();
this.taskQueue.updateTask(task);
this.emit('taskFailed', task.id, (err as Error).message);
this.emit('taskFailed', task.id, getErrorMessage(err));
}
}