@@ -7,7 +7,19 @@ export function errorFormat(error: unknown): string {
77
88 if ( typeof error === "object" && error !== null ) {
99 try {
10- return JSON . stringify ( error , null , 2 )
10+ const json = JSON . stringify ( error , null , 2 )
11+ // Plain objects whose own properties are all non-enumerable (or empty)
12+ // serialize to "{}", which prints as a useless bare `{}` on stderr.
13+ // Fall back to a custom toString first, then to ctor name + own prop names.
14+ if ( json === "{}" ) {
15+ const str = String ( error )
16+ if ( str && str !== "[object Object]" ) return str
17+ const ctor = error . constructor ?. name
18+ const prefix = ctor && ctor !== "Object" ? ctor : "Error"
19+ const names = Object . getOwnPropertyNames ( error )
20+ return names . length === 0 ? `${ prefix } (no message)` : `${ prefix } { ${ names . join ( ", " ) } }`
21+ }
22+ return json
1123 } catch {
1224 return "Unexpected error (unserializable)"
1325 }
@@ -34,7 +46,7 @@ export function errorMessage(error: unknown): string {
3446 if ( text && text !== "[object Object]" ) return text
3547
3648 const formatted = errorFormat ( error )
37- if ( formatted && formatted !== "{}" ) return formatted
49+ if ( formatted ) return formatted
3850 return "unknown error"
3951}
4052
@@ -45,15 +57,15 @@ export function errorData(error: unknown) {
4557 message : errorMessage ( error ) ,
4658 stack : error . stack ,
4759 cause : error . cause === undefined ? undefined : errorFormat ( error . cause ) ,
48- formatted : errorFormatted ( error ) ,
60+ formatted : errorFormat ( error ) ,
4961 }
5062 }
5163
5264 if ( ! isRecord ( error ) ) {
5365 return {
5466 type : typeof error ,
5567 message : errorMessage ( error ) ,
56- formatted : errorFormatted ( error ) ,
68+ formatted : errorFormat ( error ) ,
5769 }
5870 }
5971
@@ -71,12 +83,7 @@ export function errorData(error: unknown) {
7183
7284 if ( typeof data . message !== "string" ) data . message = errorMessage ( error )
7385 if ( typeof data . type !== "string" ) data . type = error . constructor ?. name
74- data . formatted = errorFormatted ( error )
86+ data . formatted = errorFormat ( error )
7587 return data
7688}
7789
78- function errorFormatted ( error : unknown ) {
79- const formatted = errorFormat ( error )
80- if ( formatted !== "{}" ) return formatted
81- return String ( error )
82- }
0 commit comments