From 1231c4a9dc0539cff7ddcc59959ee85caac12b09 Mon Sep 17 00:00:00 2001 From: Aryan Jassal Date: Thu, 7 Nov 2024 14:46:48 +1100 Subject: [PATCH] chore: added intelligent error message synthesis --- src/network/utils.ts | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/network/utils.ts b/src/network/utils.ts index ddbd48fc0..7b7cb212e 100644 --- a/src/network/utils.ts +++ b/src/network/utils.ts @@ -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