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
This commit is contained in:
Kyra
2018-08-16 20:51:21 +02:00
committed by Amish Shah
parent e2726f5a9a
commit 3f81b613d8

View File

@@ -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);