Skip to content

Commit 13c638d

Browse files
committed
chore: added error details and stack to unexpected error message
1 parent 059267b commit 13c638d

File tree

3 files changed

+42
-42
lines changed

3 files changed

+42
-42
lines changed

src/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class ErrorPolykeyCLIAsynchronousDeadlock<T> extends ErrorPolykeyCLI<T> {
8080
* If these exceptions occur, there is a bug. Most likely in Polykey.
8181
*/
8282
class ErrorPolykeyCLIUnexpectedError<T> extends ErrorPolykeyCLI<T> {
83-
static description = 'Unexpected error';
83+
static description = 'An unexpected error occured';
8484
exitCode = sysexits.SOFTWARE;
8585
}
8686

src/polykey.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ async function polykeyAgentMain(): Promise<number> {
7676
} else {
7777
// Unknown error, this should not happen
7878
const wrappedError = new binErrors.ErrorPolykeyCLIUnexpectedError(
79-
'An unexpected error occurred',
79+
binUtils.composeErrorMessage(e),
8080
{ cause: e },
8181
);
8282
process.stderr.write(
@@ -222,7 +222,7 @@ async function polykeyMain(argv: Array<string>): Promise<number> {
222222
} else {
223223
// Unknown error, this should not happen
224224
const wrappedError = new binErrors.ErrorPolykeyCLIUnexpectedError(
225-
'An unexpected error occurred',
225+
binUtils.composeErrorMessage(e),
226226
{ cause: e },
227227
);
228228
process.stderr.write(

src/utils/utils.ts

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -446,10 +446,13 @@ function outputFormatterError(err: any): string {
446446
for (const [key, value] of Object.entries(err.metadata)) {
447447
output += `${indent}${key}\t${value}\n`;
448448
}
449+
output += `${indent}timestamp\t${err.timestamp}\n`;
449450
} else {
450451
output += '\n';
451452
}
452-
output += `${indent}timestamp\t${err.timestamp}\n`;
453+
if (err.stack) {
454+
output += `${indent}stack: ${JSON.stringify(err.stack)}\n`;
455+
}
453456
output += `${indent}cause: `;
454457
err = err.cause;
455458
} else if (err instanceof ErrorPolykey) {
@@ -483,44 +486,7 @@ function outputFormatterError(err: any): string {
483486
break;
484487
}
485488
} else {
486-
let errorName = '';
487-
switch (typeof err) {
488-
case 'boolean':
489-
case 'number':
490-
case 'string':
491-
case 'bigint':
492-
case 'symbol':
493-
errorName = `Literal: ${String(err)}`;
494-
break;
495-
case 'object':
496-
if (err == null) break;
497-
if ('name' in err && typeof err.name === 'string') {
498-
errorName = err.name;
499-
break;
500-
}
501-
if (err.constructor?.name != null) {
502-
if (err.constructor.name === 'Object') {
503-
// If the constructor name is Object, then the error is a JSON
504-
// object.
505-
errorName = `Object: ${JSON.stringify(err)}`;
506-
} else {
507-
// Otherwise, it is a regular object.
508-
errorName = `Object: ${err.constructor.name}`;
509-
}
510-
break;
511-
}
512-
break;
513-
}
514-
if (errorName === '') {
515-
try {
516-
errorName = `Value: ${err}`;
517-
} catch (e) {
518-
if (e instanceof TypeError) errorName = 'Value: null';
519-
else throw e;
520-
}
521-
}
522-
523-
output += errorName;
489+
output += composeErrorMessage(err);
524490
if (err.message && err.message !== '') {
525491
output += `: ${err.message}`;
526492
}
@@ -532,6 +498,39 @@ function outputFormatterError(err: any): string {
532498
return output;
533499
}
534500

501+
function composeErrorMessage(error: any) {
502+
switch (typeof error) {
503+
case 'boolean':
504+
case 'number':
505+
case 'string':
506+
case 'bigint':
507+
case 'symbol':
508+
return `Thrown non-error literal '${String(error)}'`;
509+
case 'object':
510+
if (error == null) break;
511+
if ('name' in error && typeof error.name === 'string') {
512+
return `Thrown non-error '${error.name}'`;
513+
}
514+
if (error.constructor?.name != null) {
515+
if (error.constructor.name === 'Object') {
516+
// If the constructor name is Object, then the error is a JSON
517+
// object.
518+
return `Thrown non-error JSON '${JSON.stringify(error)}'`;
519+
} else {
520+
// Otherwise, it is a regular object.
521+
return `Thrown non-error object '${error.constructor.name}'`;
522+
}
523+
}
524+
break;
525+
}
526+
try {
527+
return `Thrown non-error value '${error}'`;
528+
} catch (e) {
529+
if (e instanceof TypeError) return `Thrown non-error value 'null'`;
530+
else throw e;
531+
}
532+
}
533+
535534
/**
536535
* CLI Authentication Retry Loop
537536
* Retries unary calls on attended authentication errors
@@ -633,6 +632,7 @@ export {
633632
outputFormatterDict,
634633
outputFormatterJson,
635634
outputFormatterError,
635+
composeErrorMessage,
636636
retryAuthentication,
637637
remoteErrorCause,
638638
encodeEscapedWrapped,

0 commit comments

Comments
 (0)