(error: unknown)
| 3 | import { INTERRUPT_MESSAGE_FOR_TOOL_USE } from './messages.js' |
| 4 | |
| 5 | export function formatError(error: unknown): string { |
| 6 | if (error instanceof AbortError) { |
| 7 | return error.message || INTERRUPT_MESSAGE_FOR_TOOL_USE |
| 8 | } |
| 9 | if (!(error instanceof Error)) { |
| 10 | return String(error) |
| 11 | } |
| 12 | const parts = getErrorParts(error) |
| 13 | const fullMessage = |
| 14 | parts.filter(Boolean).join('\n').trim() || 'Command failed with no output' |
| 15 | if (fullMessage.length <= 10000) { |
| 16 | return fullMessage |
| 17 | } |
| 18 | const halfLength = 5000 |
| 19 | const start = fullMessage.slice(0, halfLength) |
| 20 | const end = fullMessage.slice(-halfLength) |
| 21 | return `${start}\n\n... [${fullMessage.length - 10000} characters truncated] ...\n\n${end}` |
| 22 | } |
| 23 | |
| 24 | export function getErrorParts(error: Error): string[] { |
| 25 | if (error instanceof ShellError) { |
no test coverage detected