1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-22 09:23:13 +01:00
onion/js/utils/error_utils.js

53 lines
1.4 KiB
JavaScript
Raw Normal View History

2015-07-17 15:41:09 +02:00
'use strict';
import AppConstants from '../constants/application_constants';
/**
2018-05-24 10:04:45 +02:00
* Logs an error in to the console.
2015-07-17 15:41:09 +02:00
* Optionally, a comment can be defined.
* @param {Error} error a Javascript error
* @param {string} comment Will also be submitted to Sentry, but will not be logged
*/
2018-05-24 10:04:45 +02:00
function logGlobal(error, comment) {
2015-07-17 15:41:09 +02:00
console.error(error);
2016-04-13 10:19:41 +02:00
if (error.hasOwnProperty('json')) {
comment = {
...comment,
json: error.json
};
}
2015-07-17 15:41:09 +02:00
}
export function initLogging() {
console.logGlobal = logGlobal;
}
/*
* 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)
.map((errorKey) => {
return errors[errorKey];
});
// 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] || '';
}