forked from CoreBunch/Instatic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrorMessage.ts
More file actions
17 lines (17 loc) · 767 Bytes
/
Copy patherrorMessage.ts
File metadata and controls
17 lines (17 loc) · 767 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* Extract a human-readable message from an unknown thrown value.
*
* The canonical way to turn a `catch (err)` value into display/log text. Use
* this instead of hand-writing `getErrorMessage(err, '…')` at
* each call site — that pattern was duplicated ~130 times across the codebase
* before this util existed.
*
* An `Error` with an empty/whitespace-only message falls back too, so callers
* never render a blank error string.
*
* For logging the raw thrown value when it isn't an `Error`, pass `String(err)`
* as the fallback explicitly — that intent stays at the call site.
*/
export function getErrorMessage(err: unknown, fallback = 'Unknown error'): string {
return err instanceof Error && err.message.trim() ? err.message : fallback
}