Skip to content

Commit

Permalink
fix(nextjs): Fix error logging (#3512)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
lobsterkatie and HazAT authored May 7, 2021
1 parent 0f75bd7 commit eb756d2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
6 changes: 2 additions & 4 deletions packages/nextjs/src/utils/instrumentServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
}
10 changes: 6 additions & 4 deletions packages/utils/src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, <other
* args>)` or `origMethod.apply(this, [<other args>])` (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 {
Expand Down

0 comments on commit eb756d2

Please sign in to comment.