UNPKG

@soinlabs/hawk

Version:

Package to better manage errors, logs, and its notifications

62 lines (55 loc) 1.68 kB
const LoggedError = require('./LoggedError') class ErrorBuilder { static TO_ELASTIC_TEMPLATE = 'toElastic' static TO_SLACK_TEMPLATE = 'toSlack' static TO_LOG_TEMPLATE = 'toLog' static TO_EMAIL_ERROR = 'toEmail' static TO_ALL_TEMPLATE = 'toAll' static DEFAULT_TEMPLATE = 'default' /** * It is a template builder function, returns a function that allows * build a template error (to Slask, to Elasti ...) * @param {String} selectedTemplateType */ static templateBuilder(selectedTemplateType) { return (message, referenceCode) => { let template = this._selectTemplate(selectedTemplateType) if (referenceCode !== undefined && message !== undefined) { template.setMessage(message).setReferenceCode(referenceCode) } return template } } /** * Private method * It return an error and sets destination (slack, log, elastic) to error * @param {String} selectedTemplateType * @return {LoggedError} */ static _selectTemplate(selectedTemplateType) { let error = new LoggedError() switch (selectedTemplateType) { case this.TO_ELASTIC_TEMPLATE: error.setToLog(true) error.setToElastic(true) break case this.TO_SLACK_TEMPLATE: error.setToSlack(true) break case this.TO_LOG_TEMPLATE: error.setToLog(true) break case this.TO_EMAIL_ERROR: error.setToEmail(true) break case this.TO_ALL_TEMPLATE: error.setToSlack(true) error.setToLog(true) error.setToElastic(true) error.setToEmail(true) break } return error } } module.exports = ErrorBuilder