(
stage: string,
err: unknown,
options: {
notFound?: string;
extraHints?: Record<string, string>;
suggestion?: string;
} = {},
)
| 49 | */ |
| 50 | // fallow-ignore-next-line complexity |
| 51 | export function reportApiError( |
| 52 | stage: string, |
| 53 | err: unknown, |
| 54 | options: { |
| 55 | notFound?: string; |
| 56 | extraHints?: Record<string, string>; |
| 57 | suggestion?: string; |
| 58 | } = {}, |
| 59 | ): never { |
| 60 | const hints = { ...ERROR_CODE_HINTS, ...options.extraHints }; |
| 61 | if (err instanceof HyperframesApiError) { |
| 62 | if (err.status === 404 && options.notFound) { |
| 63 | errorBox("Not found", options.notFound); |
| 64 | process.exit(1); |
| 65 | } |
| 66 | const hint = err.code ? hints[err.code] : undefined; |
| 67 | const title = `${stage} (HTTP ${err.status})`; |
| 68 | // Priority: code-specific hint > caller suggestion > bare code |
| 69 | // label > no third line. Code-specific hints win because they |
| 70 | // address the specific failure mode; the caller suggestion is a |
| 71 | // generic-context fallback. |
| 72 | if (hint) { |
| 73 | errorBox(title, err.message, hint); |
| 74 | } else if (options.suggestion) { |
| 75 | errorBox(title, err.message, options.suggestion); |
| 76 | } else if (err.code) { |
| 77 | errorBox(title, err.message, `code: ${err.code}`); |
| 78 | } else { |
| 79 | errorBox(title, err.message); |
| 80 | } |
| 81 | process.exit(1); |
| 82 | } |
| 83 | if (err instanceof Error) { |
| 84 | errorBox(stage, err.message, options.suggestion); |
| 85 | process.exit(1); |
| 86 | } |
| 87 | errorBox(stage, String(err), options.suggestion); |
| 88 | process.exit(1); |
| 89 | } |
no test coverage detected