From 3f81b613d83140cdf3d99b93ea74d83c52846b2c Mon Sep 17 00:00:00 2001 From: Kyra Date: Thu, 16 Aug 2018 20:51:21 +0200 Subject: [PATCH] fix: Handle async stacks correctly (#2744) * fix: Capture stack traces in APIRouter to preserve async stack * fix: Handle the stack trace better * fix: Check if error is an instance of Error (5XX rejects with Result) * fix: Error.captureStackTrace is not supported in all browsers --- src/rest/APIRouter.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/rest/APIRouter.js b/src/rest/APIRouter.js index 6e0fb36a9..830c8501b 100644 --- a/src/rest/APIRouter.js +++ b/src/rest/APIRouter.js @@ -11,13 +11,27 @@ function buildRoute(manager) { get(target, name) { if (reflectors.includes(name)) return () => route.join('/'); if (methods.includes(name)) { + // Preserve async stack + let stackTrace = null; + if (Error.captureStackTrace) { + stackTrace = {}; + Error.captureStackTrace(stackTrace, this.get); + } + return options => manager.request(name, route.join('/'), Object.assign({ versioned: manager.versioned, route: route.map((r, i) => { if (/\d{16,19}/g.test(r)) return /channels|guilds/.test(route[i - 1]) ? r : ':id'; return r; }).join('/'), - }, options)); + }, options)).catch(error => { + if (stackTrace && (error instanceof Error)) { + stackTrace.name = error.name; + stackTrace.message = error.message; + error.stack = stackTrace.stack; + } + throw error; + }); } route.push(name); return new Proxy(noop, handler);