UNPKG

@cognigy/rest-api-client

Version:

Cognigy REST-Client

110 lines 3.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseError = exports.setErrorTracer = exports.errorLogger = void 0; /* Custom Modules */ const HttpStatusCode_1 = require("../helper/HttpStatusCode"); exports.errorLogger = { log: Function.prototype }; let tracer = null; /** * Set an instance of a Tracer, e.g. a Sentry Tracer, * to be used to send errors to the tracing provider */ function setErrorTracer(t) { if (t.getIsErrorTracingEnabled()) { tracer = t; } } exports.setErrorTracer = setErrorTracer; class BaseError extends Error { constructor({ name, message, code, httpStatusCode, httpStatusText, stack, meta, details, logLevel }) { var _a; super(message || name); this.name = name; this.code = code; this.httpStatusCode = httpStatusCode || HttpStatusCode_1.HttpStatusCode.INTERNAL_SERVER_ERROR; this.httpStatusText = httpStatusText || "Internal Server Error"; this.loggerstack = stack; this.details = details || {}; this.meta = meta || {}; this.logLevel = logLevel || "error"; if (this.meta.originalError) { if (this.meta.originalError instanceof BaseError) { // if the passed error is already an instance of our base class error class, no need to this.originalErrorDetails = this.meta.originalError.originalErrorDetails; } else { this.originalErrorDetails = this.parseOriginalError(this.meta.originalError); } this.meta.originalError = this.originalErrorDetails; } if (tracer) { if (this.logLevel === "error" || this.logLevel === "fatal") { const { projectId, organisationId, organisation, groupingKey, organisationName, projectName, } = this.meta; tracer.captureException(this, { traceId: (_a = this.loggerstack) === null || _a === void 0 ? void 0 : _a.traceId, organisationId: organisationId || organisation, projectId, groupingKey, organisationName, projectName, }); } } exports.errorLogger.log(this.logLevel, stack, message || name, this.meta); } parseOriginalError(error) { return { message: error.message, stack: error.stack, name: error.name, code: error.code, data: error.data, path: error.path }; } toErrorLogDetails() { return { name: this.name, code: this.code, httpStatusCode: this.httpStatusCode, httpStatusText: this.httpStatusText, loggerstack: this.loggerstack, details: this.details, meta: this.meta, originalError: this.originalErrorDetails, stack: this.stack }; } toResponse() { return { error: { code: this.code, message: this.message || this.name, loggerstack: this.loggerstack, meta: this.meta, details: this.details, logLevel: this.logLevel, } }; } toRFC7807Response(data) { const { traceId = "", path = "" } = data || {}; const result = { type: "about:blank", title: this.httpStatusText, detail: this.message || this.name, instance: path, status: this.httpStatusCode, traceId: this.loggerstack && this.loggerstack.traceId || traceId, code: this.code, }; if (this.details) { result["details"] = this.details; } return result; } } exports.BaseError = BaseError; //# sourceMappingURL=baseError.js.map