-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.ts
More file actions
29 lines (25 loc) · 757 Bytes
/
Copy patherrors.ts
File metadata and controls
29 lines (25 loc) · 757 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
export class CommandError extends Error {
code?: string;
constructor(message: string, code?: string) {
super(message);
this.name = 'CommandError';
this.code = code;
}
}
function hasMessage(value: unknown): value is { message: string; code?: string } {
return typeof value === 'object'
&& value !== null
&& 'message' in value
&& typeof (value as { message: unknown }).message === 'string';
}
export function toErrorMessage(error: unknown): string {
const raw = hasMessage(error)
? error.message
: error instanceof Error
? error.message
: String(error ?? 'Unknown error');
return raw
.replace(/^Error:\s*/i, '')
.replace(/^Command\s+\w+\s+failed:\s*/i, '')
.trim() || 'Unknown error';
}