UNPKG

@soinlabs/hawk

Version:

Package to better manage errors, logs, and its notifications

136 lines (119 loc) 3.19 kB
const { SimpleError } = require('@soinlabs/errors') const StepGetter = require('./StepGetter') class LoggedError extends SimpleError { static LOW_LEVEL = 'low' static MEDIUM_LEVEL = 'medium' static HIGH_LEVEL = 'high' constructor(fields) { super(fields) this.message = '' /** * @deprecated since version 0.1.0 use status instead */ this.code = 500 this.status = 500 this.errorLevel = 'low' this.referenceCode = '' this.step = this._getBuiltStep() this.stepDetails = this._getStepDetails() this.previousError = null this.previousErrorDetails = null this.toLog = true this.toElastic = false this.toSlack = false this.toEmail = false this.emailOptions = {} /* *it fill the class fields with JSON object, if the object exist *if object is null or undefined, then the class fileds preserves default values *if a class field doesn't match with any json property, then it preserves his default value *if the user adds a new property that is not an original class field, then this property can be *accessed like a class field and can be used by logger */ this._initializeWithJSON(fields) this._tryToGetPreviousErrorDetails() } /** * @deprecated since version 0.1.0 use setStatus instead */ setCode(code) { this.code = code this.status = this.code return this } setStatus(status) { this.status = status return this } setMessage(message) { this.message = message return this } setErrorLevel(errorLevel) { this.errorLevel = errorLevel return this } setReferenceCode(referenceCode) { this.referenceCode = referenceCode return this } setStep(step) { this.step = step return this } setPreviousError(previousError) { this.previousError = previousError this._tryToGetPreviousErrorDetails() return this } setToLog(toLog) { this.toLog = toLog return this } setToElastic(toElastic) { this.toElastic = toElastic return this } setToSlack(toSlack) { this.toSlack = toSlack return this } setToEmail(toEmail) { this.toEmail = toEmail return this } setEmailOptions(emailOptions) { this.setEmailOptions = emailOptions return this } _initializeWithJSON(json) { if (!(json === undefined || json === null)) { for (let propertieName in json) { this[propertieName] = json[propertieName] } } } _getBuiltStep() { const stepGetter = new StepGetter(this) const finalStep = stepGetter.getFinalStep() let pathStep = '' if (finalStep !== null && finalStep !== undefined) { pathStep = finalStep.calledFunction + '-' + finalStep.path } return pathStep } _getStepDetails() { const stepGetter = new StepGetter(this) return stepGetter.getStepTrace() } _tryToGetPreviousErrorDetails() { if (this.previousError) { const stepGetter = new StepGetter(this.previousError) this.previousErrorDetails = { message: this.previousError.message, stack: stepGetter.getStepTrace(), } } } } module.exports = LoggedError