devebot
Version:
Nodejs Microservice Framework
35 lines (34 loc) • 1.15 kB
JavaScript
;
const nodash = require("./nodash");
const MAPPINGS = {
"MODULE_NOT_FOUND": "Module not found"
};
function _logit(L, level) {
L = nodash.isObject(L) && nodash.isFunction(L.has) && nodash.isFunction(L.log) && L || null;
L && L.has(level) && L.log.apply(Array.prototype.slice.call(arguments, 1));
}
function loader(name, opts = {}) {
const _ref_ = {
pkg: {}
};
try {
_ref_.pkg = require(name); // eslint-disable-line
_logit(opts.logger, "debug", " - file %s is loading ... ok", name);
} catch (err) {
if (err.code) {
if (MAPPINGS[err.code]) {
_logit(opts.logger, "debug", " - file %s is loading ... failed. Reason: %s", name, MAPPINGS[err.code]);
} else {
_logit(opts.logger, "debug", " - file %s is loading ... failed. Error code: %s", name, err.code);
}
} else {
_logit(opts.logger, "debug", " - file %s is loading ... failed. Error message: %s", name, err.message);
}
if (opts.stopWhenError) {
_logit(opts.logger, "error", " - loading module file [%s] throw error.", name);
throw err;
}
}
return _ref_.pkg;
}
module.exports = loader;