File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 1+ export type ErrorWithMessage = {
2+ message : string
3+ }
4+
5+ function isErrorWithMessage ( error : unknown ) : error is ErrorWithMessage {
6+ return (
7+ typeof error === 'object' &&
8+ error !== null &&
9+ 'message' in error &&
10+ typeof ( error as Record < string , unknown > ) . message === 'string'
11+ )
12+ }
13+
14+ function toErrorWithMessage ( maybeError : unknown ) : ErrorWithMessage {
15+ if ( isErrorWithMessage ( maybeError ) ) return maybeError
16+
17+ try {
18+ return new Error ( JSON . stringify ( maybeError ) )
19+ } catch {
20+ // fallback in case there's an error stringifying the maybeError
21+ // like with circular references for example.
22+ return new Error ( String ( maybeError ) )
23+ }
24+ }
25+
26+ export function getErrorMessage ( error : unknown ) {
27+ return toErrorWithMessage ( error ) . message
28+ }
You can’t perform that action at this time.
0 commit comments