@grafana/faro-core
Version:
Core package of Faro.
93 lines • 5.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.initializeExceptionsAPI = initializeExceptionsAPI;
const transports_1 = require("../../transports");
const utils_1 = require("../../utils");
const date_1 = require("../../utils/date");
const initialize_1 = require("../userActions/initialize");
const utils_2 = require("../utils");
const const_1 = require("./const");
let stacktraceParser;
function initializeExceptionsAPI({ internalLogger, config, metas, transports, tracesApi, userActionsApi, }) {
var _a;
internalLogger.debug('Initializing exceptions API');
let lastPayload = null;
stacktraceParser = (_a = config.parseStacktrace) !== null && _a !== void 0 ? _a : stacktraceParser;
const changeStacktraceParser = (newStacktraceParser) => {
internalLogger.debug('Changing stacktrace parser');
stacktraceParser = newStacktraceParser !== null && newStacktraceParser !== void 0 ? newStacktraceParser : stacktraceParser;
};
const getStacktraceParser = () => stacktraceParser;
const { ignoreErrors = [], preserveOriginalError } = config;
const pushError = (error, { skipDedupe, stackFrames, type, context, spanContext, timestampOverwriteMs, originalError, fingerprint, fatal, } = {}) => {
var _a;
if (isErrorIgnored(ignoreErrors, originalError !== null && originalError !== void 0 ? originalError : error)) {
return;
}
try {
const ctx = (0, utils_1.stringifyObjectValues)(Object.assign(Object.assign({}, parseCause(originalError !== null && originalError !== void 0 ? originalError : error)), (context !== null && context !== void 0 ? context : {})));
const item = {
meta: metas.value,
payload: Object.assign(Object.assign(Object.assign(Object.assign({ type: type || error.name || const_1.defaultExceptionType, value: error.message, timestamp: timestampOverwriteMs ? (0, date_1.timestampToIsoString)(timestampOverwriteMs) : (0, utils_1.getCurrentTimestamp)(), trace: spanContext
? {
trace_id: spanContext.traceId,
span_id: spanContext.spanId,
}
: tracesApi.getTraceContext() }, ((0, utils_1.isEmpty)(ctx) ? {} : { context: ctx })), (preserveOriginalError ? { originalError } : {})), (fingerprint ? { fingerprint } : {})), (fatal !== undefined ? { fatal } : {})),
type: transports_1.TransportItemType.EXCEPTION,
};
stackFrames = stackFrames !== null && stackFrames !== void 0 ? stackFrames : (error.stack ? stacktraceParser === null || stacktraceParser === void 0 ? void 0 : stacktraceParser(error).frames : undefined);
if (stackFrames === null || stackFrames === void 0 ? void 0 : stackFrames.length) {
item.payload.stacktrace = {
frames: stackFrames,
};
}
const testingPayload = {
type: item.payload.type,
value: item.payload.value,
stacktrace: item.payload.stacktrace,
context: item.payload.context,
fingerprint: item.payload.fingerprint,
fatal: (_a = item.payload.fatal) !== null && _a !== void 0 ? _a : false,
};
if (!skipDedupe && config.dedupe && !(0, utils_1.isNull)(lastPayload) && (0, utils_1.deepEqual)(testingPayload, lastPayload)) {
internalLogger.debug('Skipping error push because it is the same as the last one\n', item.payload);
return;
}
lastPayload = testingPayload;
internalLogger.debug('Pushing exception\n', item);
if (!(0, initialize_1.addItemToUserActionBuffer)(userActionsApi.getActiveUserAction(), item)) {
transports.execute(item);
}
}
catch (err) {
internalLogger.error('Error pushing event', err);
}
};
changeStacktraceParser(config.parseStacktrace);
return {
changeStacktraceParser,
getStacktraceParser,
pushError,
};
}
function parseCause(error) {
let cause = error.cause;
if ((0, utils_1.isError)(cause)) {
cause = error.cause.toString();
// typeof operator on null returns "object". This is a well-known quirk in JavaScript and is considered a bug that cannot be fixed due to backward compatibility issues.
// MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof#typeof_null
}
else if (cause !== null && ((0, utils_1.isObject)(error.cause) || (0, utils_1.isArray)(error.cause))) {
cause = (0, utils_1.stringifyExternalJson)(error.cause);
}
else if (cause != null) {
cause = error.cause.toString();
}
return cause == null ? {} : { cause };
}
function isErrorIgnored(ignoreErrors, error) {
const { message, name, stack } = error;
return (0, utils_2.shouldIgnoreEvent)(ignoreErrors, message + ' ' + name + ' ' + stack);
}
//# sourceMappingURL=initialize.js.map