From eb756d2be6a4058049973d9d737eb2309a096bbf Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Fri, 7 May 2021 04:27:50 -0700 Subject: [PATCH] fix(nextjs): Fix error logging (#3512) * s/bind/call and switch to real function to preserve this value * add note to fill function * Update instrumentServer.ts Co-authored-by: Daniel Griesser --- packages/nextjs/src/utils/instrumentServer.ts | 6 ++---- packages/utils/src/object.ts | 10 ++++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/nextjs/src/utils/instrumentServer.ts b/packages/nextjs/src/utils/instrumentServer.ts index dabc48f52ea6..106a822d30ea 100644 --- a/packages/nextjs/src/utils/instrumentServer.ts +++ b/packages/nextjs/src/utils/instrumentServer.ts @@ -83,11 +83,9 @@ function makeWrappedHandlerGetter(origHandlerGetter: HandlerGetter): WrappedHand * @returns A wrapped version of that logger */ function makeWrappedErrorLogger(origErrorLogger: ErrorLogger): WrappedErrorLogger { - return (err: Error): void => { + return function(this: Server, err: Error): void { // TODO add context data here Sentry.captureException(err); - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - return origErrorLogger.bind(this, err); + return origErrorLogger.call(this, err); }; } diff --git a/packages/utils/src/object.ts b/packages/utils/src/object.ts index 3a41d71c3107..da099a17979b 100644 --- a/packages/utils/src/object.ts +++ b/packages/utils/src/object.ts @@ -8,12 +8,14 @@ import { getFunctionName } from './stacktrace'; import { truncate } from './string'; /** - * Wrap a given object method with a higher-order function + * Replace a method in an object with a wrapped version of itself. * * @param source An object that contains a method to be wrapped. - * @param name A name of method to be wrapped. - * @param replacementFactory A function that should be used to wrap a given method, returning the wrapped method which - * will be substituted in for `source[name]`. + * @param name The name of the method to be wrapped. + * @param replacementFactory A higher-order function that takes the original version of the given method and returns a + * wrapped verstion. Note: The function returned by `replacementFactory` needs to be a non-arrow function, in order to + * preserve the correct value of `this`, and the original method must be called using `origMethod.call(this, )` or `origMethod.apply(this, [])` (rather than being called directly), again to preserve `this`. * @returns void */ export function fill(source: { [key: string]: any }, name: string, replacementFactory: (...args: any[]) => any): void {