Skip to content

Commit

Permalink
chore: added intelligent error message synthesis
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Nov 7, 2024
1 parent e8a90e1 commit 1231c4a
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions src/network/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,13 +482,37 @@ function fromError(error: any) {
return wrappedError.toJSON();
}

// If an error is not serialisable (for example, throwing Object.create(null))
// then trying to serialise it to a string would throw an error.
// Use the properties of the error to create an appropriate error message.
let message = '';
switch (typeof error) {
case 'boolean':
case 'number':
case 'string':
case 'bigint':
case 'symbol':
message = `Non-error literal ${String(error)} was thrown`;
case 'object':
// Let the fallback handler catch null values.
if (error == null) break;
// If we have an error message defined, then return that.
if ('message' in error && typeof error.message === 'string') {
message = error.message;
}
// If present, mention the constructor name in the message.
if (error.constructor?.name != null) {
message = `Non-error object ${error.constructor.name} was thrown`;
}
// Any other values should be handled by the fallback handler.
break;
}

// Handle cases where the error is not serialisable, like objects created
// using Object.create(null). Trying to serialise this throws a TypeError.
try {
message = `Unexpected error occured: ${error}`;
message = `Non-error value ${String(error)} was thrown`;
} catch (e) {
message = 'Unexpected error occured';
if (e instanceof TypeError) message = 'Non-error value was thrown';
else throw e;
}

// If the error was not an Error, then wrap it inside ErrorPolykeyUnexpected
Expand Down

0 comments on commit 1231c4a

Please sign in to comment.