How to report console.error with Sentry?

帅比萌擦擦* 提交于 2020-01-01 05:38:05

问题


I have application where some critical issues are reported with console.error but are not thrown so application might continue to run - possibly in crippled state.

It's necessary to report also console.error issues, but Sentry (Raven) library send to server only thrown exceptions.

Does someone knows how to solve this nicely ?

(ideally without need to rewrite all console.error calls, cause also some vendor libraries might still write output just into console)


回答1:


As user @kumar303 mentioned in his comment to the question ... you can use the JS console integration Sentry.Integrations.CaptureConsole.

See https://docs.sentry.io/platforms/javascript/?platform=browsernpm#captureconsole for documentation.

At the end you JS code to setup Sentry looks as follows:

Sentry.init({
  dsn: 'https://your-sentry-server-dsn',
  integrations: [
    new Integrations.CaptureConsole({
      levels: ['error']
    })
  ],
  release: '1.0.0',
  environment: 'prod',
  maxBreadcrumbs: 50
})

If then someone calls console.error a new event will sent to sentry.




回答2:


Found a little hacky solution:

const consoleError = console.error;
console.error = function(firstParam) {
  const response = consoleError.apply(console, arguments);
  Raven.captureException(firstParam, { level: 'error' });
  return response;
};

It just wraps console.error and report each of error logs in console to Raven (Sentry).

If someone have nicer approach (maybe some hidden feature of Sentry) please feel free to share!




回答3:


Here's a more robust override solution

// creating function declarations for better stacktraces (otherwise they'd be anonymous function expressions)
var oldConsoleError = console.error;
console.error = reportingConsoleError; // defined via function hoisting
function reportingConsoleError() {
  var args = Array.prototype.slice.call(arguments);
  Sentry.captureException(reduceConsoleArgs(args), { level: 'error' });
  return oldConsoleError.apply(console, args);
};

var oldConsoleWarn = console.warn;
console.warn = reportingConsoleWarn; // defined via function hoisting
function reportingConsoleWarn() {
  var args = Array.prototype.slice.call(arguments);
  Sentry.captureMessage(reduceConsoleArgs(args), { level: 'warning' });
  return oldConsoleWarn.apply(console, args);
}

function reduceConsoleArgs(args) {
  let errorMsg = args[0];
  // Make sure errorMsg is either an error or string.
  // It's therefore best to pass in new Error('msg') instead of just 'msg' since
  // that'll give you a stack trace leading up to the creation of that new Error
  // whereas if you just pass in a plain string 'msg', the stack trace will include
  // reportingConsoleError and reportingConsoleCall
  if (!(errorMsg instanceof Error)) {
    // stringify all args as a new Error (which creates a stack trace)
    errorMsg = new Error(
      args.reduce(function(accumulator, currentValue) {
        return accumulator.toString() + ' ' + currentValue.toString();
      }, '')
    );
  }
  return errorMsg;
}


来源:https://stackoverflow.com/questions/50633580/how-to-report-console-error-with-sentry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!