Skip to content

Commit d400d53

Browse files
committed
Stringify error cause if it's not an Error instance
1 parent ed39850 commit d400d53

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

src/common/utils.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,19 +138,42 @@ export function getServiceURL(gitpodHost: string): string {
138138
return new URL(gitpodHost).toString().replace(/\/$/, '');
139139
}
140140

141+
export default function isPlainObject(value: any) {
142+
if (typeof value !== 'object' || value === null) {
143+
return false;
144+
}
145+
146+
const prototype = Object.getPrototypeOf(value);
147+
return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null);
148+
}
149+
141150
export class WrapError extends Error {
142151
constructor(
143152
msg: string,
144153
readonly cause: any,
145154
readonly code?: string
146155
) {
147-
const isErr = cause instanceof Error;
148-
super(isErr ? `${msg}: ${cause.message}` : `${msg}: ${cause}`);
149-
if (isErr) {
156+
super();
157+
158+
let originalMessage = cause?.message;
159+
if (!originalMessage) {
160+
if (isPlainObject(cause)) {
161+
try {
162+
originalMessage = JSON.stringify(cause);
163+
} catch {
164+
}
165+
} else {
166+
originalMessage = cause?.toString();
167+
}
168+
}
169+
this.message = `${msg}: ${originalMessage}`;
170+
171+
if (cause instanceof Error) {
150172
this.name = cause.name;
151173
this.stack = this.stack + '\n\n' + cause.stack;
152174
}
153-
this.code ??= cause.code;
175+
176+
this.code ??= cause?.code;
154177
}
155178
}
156179

0 commit comments

Comments
 (0)