Skip to content

Commit eb756d2

Browse files
lobsterkatieHazAT
andauthored
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 <[email protected]>
1 parent 0f75bd7 commit eb756d2

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

packages/nextjs/src/utils/instrumentServer.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,9 @@ function makeWrappedHandlerGetter(origHandlerGetter: HandlerGetter): WrappedHand
8383
* @returns A wrapped version of that logger
8484
*/
8585
function makeWrappedErrorLogger(origErrorLogger: ErrorLogger): WrappedErrorLogger {
86-
return (err: Error): void => {
86+
return function(this: Server, err: Error): void {
8787
// TODO add context data here
8888
Sentry.captureException(err);
89-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
90-
// @ts-ignore
91-
return origErrorLogger.bind(this, err);
89+
return origErrorLogger.call(this, err);
9290
};
9391
}

packages/utils/src/object.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import { getFunctionName } from './stacktrace';
88
import { truncate } from './string';
99

1010
/**
11-
* Wrap a given object method with a higher-order function
11+
* Replace a method in an object with a wrapped version of itself.
1212
*
1313
* @param source An object that contains a method to be wrapped.
14-
* @param name A name of method to be wrapped.
15-
* @param replacementFactory A function that should be used to wrap a given method, returning the wrapped method which
16-
* will be substituted in for `source[name]`.
14+
* @param name The name of the method to be wrapped.
15+
* @param replacementFactory A higher-order function that takes the original version of the given method and returns a
16+
* wrapped verstion. Note: The function returned by `replacementFactory` needs to be a non-arrow function, in order to
17+
* preserve the correct value of `this`, and the original method must be called using `origMethod.call(this, <other
18+
* args>)` or `origMethod.apply(this, [<other args>])` (rather than being called directly), again to preserve `this`.
1719
* @returns void
1820
*/
1921
export function fill(source: { [key: string]: any }, name: string, replacementFactory: (...args: any[]) => any): void {

0 commit comments

Comments
 (0)