From a88a494f7a0eb767a1f0b8649e4ed9765a23d5c3 Mon Sep 17 00:00:00 2001 From: David Teller Date: Fri, 7 Jan 2022 11:00:44 +0100 Subject: [PATCH] Let's not overwrite the error message when we're fixing the stack. --- src/utils.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index df76d1c..4e9dbc1 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -226,7 +226,9 @@ function patchMatrixClientForConciseExceptions() { } let originalRequestFn = getRequestFn(); setRequestFn((params, cb) => { - let stack = new Error().stack; + // Store an error early, to maintain *some* semblance of stack. + // We'll only throw the error if there is one. + let error = new Error("STACK CAPTURE"); originalRequestFn(params, function conciseExceptionRequestFn(err, response, resBody) { if (!err && (response?.statusCode < 200 || response?.statusCode >= 300)) { // Normally, converting HTTP Errors into rejections is done by the caller @@ -280,8 +282,8 @@ function patchMatrixClientForConciseExceptions() { if ("body" in err) { body = (err as any).body; } - let error = new Error(`Error during MatrixClient request ${method} ${path}: ${err.statusCode} ${err.statusMessage} -- ${body}`); - error.stack = stack; + let message = `Error during MatrixClient request ${method} ${path}: ${err.statusCode} ${err.statusMessage} -- ${body}`; + error.message = message; if (body) { // Calling code may use `body` to check for errors, so let's // make sure that we're providing it. @@ -299,6 +301,7 @@ function patchMatrixClientForConciseExceptions() { // Calling code may use `statusCode` to check for errors, so let's // make sure that we're providing it. if ("statusCode" in err) { + // Define the property but don't make it visible during logging. Object.defineProperty(error, "statusCode", { value: err.statusCode, enumerable: false,