Skip to content

Commit

Permalink
🔧 Ignore irrelevant errors (Sentry)
Browse files Browse the repository at this point in the history
  • Loading branch information
cermakjiri committed Oct 30, 2024
1 parent 1eec7c1 commit 4a45c22
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
14 changes: 14 additions & 0 deletions examples/simplewebauthn/src/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@ import { createLogger } from '@workspace/logger';

import { env } from '~env';

const ignoredErrorMessages = [
'The operation either timed out or was not allowed.',
'The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission',
'The authenticator was previously registered',
'Failed to fetch',
] as const;

export const logger = createLogger('app', {
outputToConsole: env.NEXT_PUBLIC_NODE_ENV !== 'production',
captureExceptionFilter: error => {
if (ignoredErrorMessages.some(ignoredErrorMessage => ignoredErrorMessage.startsWith(error.message))) {
return false;
}

return true;
},
});
9 changes: 8 additions & 1 deletion packages/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export function createLogger(
name: string,
props: {
outputToConsole: boolean;
/**
* If returns true, the error will be captured by Sentry.
*/
captureExceptionFilter?: (error: Error) => boolean;
},
) {
const logger = getLogger(name);
Expand All @@ -17,6 +21,7 @@ export function createLogger(
}

const logError = logger.error;
const { captureExceptionFilter = () => true } = props;

return {
...logger,
Expand All @@ -25,7 +30,9 @@ export function createLogger(
setExtras(extras);
}

captureException(error);
if (captureExceptionFilter(error)) {
captureException(error);
}

logError(error, extras);
},
Expand Down

0 comments on commit 4a45c22

Please sign in to comment.