2015-07-17 15:41:09 +02:00
|
|
|
import Raven from 'raven-js';
|
|
|
|
|
|
|
|
import AppConstants from '../constants/application_constants';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs an error in to the console but also sends it to
|
|
|
|
* Sentry.
|
|
|
|
* Optionally, a comment can be defined.
|
|
|
|
* @param {Error} error a Javascript error
|
|
|
|
* @param {boolean} ignoreSentry Defines whether or not the error should be submitted to Sentry
|
|
|
|
* @param {string} comment Will also be submitted to Sentry, but will not be logged
|
|
|
|
*/
|
2016-01-08 14:49:39 +01:00
|
|
|
function logGlobal(error, comment, ignoreSentry) {
|
2015-07-17 15:41:09 +02:00
|
|
|
console.error(error);
|
|
|
|
|
2016-04-13 10:19:41 +02:00
|
|
|
if (error.hasOwnProperty('json')) {
|
2016-06-24 16:11:43 +02:00
|
|
|
comment.json = error.json;
|
2016-04-13 10:19:41 +02:00
|
|
|
}
|
|
|
|
|
2016-01-08 14:49:39 +01:00
|
|
|
if (!ignoreSentry) {
|
|
|
|
Raven.captureException(error, comment ? { extra: { comment } } : undefined);
|
2015-07-17 15:41:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function initLogging() {
|
2016-06-24 16:11:43 +02:00
|
|
|
const { raven: { ignoreErrors, url: ravenUrl }, version } = AppConstants;
|
|
|
|
|
2015-07-17 15:41:09 +02:00
|
|
|
// Initialize Raven for logging on Sentry
|
2016-06-24 16:11:43 +02:00
|
|
|
Raven.config(ravenUrl, {
|
|
|
|
ignoreErrors,
|
|
|
|
release: version,
|
2015-07-17 15:41:09 +02:00
|
|
|
}).install();
|
|
|
|
|
|
|
|
window.onerror = Raven.process;
|
|
|
|
|
|
|
|
console.logGlobal = logGlobal;
|
2015-11-23 17:12:47 +01:00
|
|
|
}
|
2015-12-16 18:14:06 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Gets the json errors from the error as an array
|
|
|
|
* @param {Error} error A Javascript error
|
|
|
|
* @return {Array} List of json errors
|
|
|
|
*/
|
|
|
|
export function getJsonErrorsAsArray(error) {
|
|
|
|
const { json: { errors = {} } = {} } = error;
|
|
|
|
|
|
|
|
const errorArrays = Object
|
|
|
|
.keys(errors)
|
2016-06-24 16:11:43 +02:00
|
|
|
.map((errorKey) => errors[errorKey]);
|
2015-12-16 18:14:06 +01:00
|
|
|
|
|
|
|
// Collapse each errorKey's errors into a flat array
|
|
|
|
return [].concat(...errorArrays);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tries to get an error message from the error, either by using its notification
|
|
|
|
* property or first json error (if any)
|
|
|
|
* @param {Error} error A Javascript error
|
|
|
|
* @return {string} Error message string
|
|
|
|
*/
|
|
|
|
export function getErrorNotificationMessage(error) {
|
|
|
|
return (error && error.notification) || getJsonErrorsAsArray(error)[0] || '';
|
|
|
|
}
|