@woocommerce/explat
Version:
WooCommerce component and utils for A/B testing.
43 lines (42 loc) • 1.46 kB
JavaScript
/**
* Internal dependencies
*/
import { isDevelopmentMode } from './utils';
export const logError = (error) => {
var _a, _b;
const onLoggingError = (e) => {
if (isDevelopmentMode) {
console.error('[ExPlat] Unable to send error to server:', e); // eslint-disable-line no-console
}
};
try {
const { message, ...properties } = error;
const logStashError = {
message,
properties: {
...properties,
context: 'explat',
explat_client: 'woocommerce',
},
};
if (isDevelopmentMode) {
console.error('[ExPlat] ', error.message, error); // eslint-disable-line no-console
}
else {
if (!((_a = window.wcTracks) === null || _a === void 0 ? void 0 : _a.isEnabled) && !((_b = window === null || window === void 0 ? void 0 : window._wca) === null || _b === void 0 ? void 0 : _b.push)) {
throw new Error(`Tracking is disabled, can't send error to the server`);
}
const body = new window.FormData();
body.append('error', JSON.stringify(logStashError));
window
.fetch('https://public-api.wordpress.com/rest/v1.1/js-error', {
method: 'POST',
body,
})
.catch(onLoggingError);
}
}
catch (e) {
onLoggingError(e);
}
};