Skip to content

Instantly share code, notes, and snippets.

@joeyred
Last active December 9, 2020 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeyred/4ed427918bcc25c3b7da7a2a268c9735 to your computer and use it in GitHub Desktop.
Save joeyred/4ed427918bcc25c3b7da7a2a268c9735 to your computer and use it in GitHub Desktop.
Error reporter wrapper for Sentry to prevent reporting to Sentry in dev, but still logging any resulting message or error.
/* eslint-disable no-console */
import * as Sentry from '@sentry/browser';
let production = false;
/**
* Initialize reporter. This should be done as soon as possible.
* @method initReporter
* @param {Boolean} isProduction - If the app is currently running in
* production or not.
* @param {Object} sentryOptions - Config object passed to Sentry init.
*/
export const initReporter = (isProduction, sentryOptions) => {
// Set production boolean.
production = isProduction;
// init Sentry
if (production) {
Sentry.init(sentryOptions);
}
}
/**
* @typedef {"fatal" | "error" | "warning" | "info" | "debug"} MessageLevel
*/
/**
* Takes the passed message and either sends it to Sentry or logs it to console
* depending on the node environment
*
* @method reportMessage
* @param {String} message - The message to be logged
* @param {MessageLevel} [level='info'] - The level of the message
*/
export const reportMessage = (message, level = 'info') => {
if (production) {
Sentry.captureMessage(message, level);
} else {
console.log(message);
}
}
/**
* Takes the passed error and either logs it to console, or sends it to Sentry.
*
* @method reportError
* @param {Object} error - the error object to report.
*/
export const reportError = (error) => {
if (production) {
Sentry.captureException(error);
} else {
console.log(error);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment