errors.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* eslint no-undefined: 0 */
  2. 'use strict';
  3. function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
  4. function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
  5. function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
  6. function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
  7. var format = require('./format');
  8. var _require = require('triple-beam'),
  9. LEVEL = _require.LEVEL,
  10. MESSAGE = _require.MESSAGE;
  11. /*
  12. * function errors (info)
  13. * If the `message` property of the `info` object is an instance of `Error`,
  14. * replace the `Error` object its own `message` property.
  15. *
  16. * Optionally, the Error's `stack` and/or `cause` properties can also be appended to the `info` object.
  17. */
  18. module.exports = format(function (einfo, _ref) {
  19. var stack = _ref.stack,
  20. cause = _ref.cause;
  21. if (einfo instanceof Error) {
  22. var info = Object.assign({}, einfo, _defineProperty(_defineProperty(_defineProperty({
  23. level: einfo.level
  24. }, LEVEL, einfo[LEVEL] || einfo.level), "message", einfo.message), MESSAGE, einfo[MESSAGE] || einfo.message));
  25. if (stack) info.stack = einfo.stack;
  26. if (cause) info.cause = einfo.cause;
  27. return info;
  28. }
  29. if (!(einfo.message instanceof Error)) return einfo;
  30. // Assign all enumerable properties and the
  31. // message property from the error provided.
  32. var err = einfo.message;
  33. Object.assign(einfo, err);
  34. einfo.message = err.message;
  35. einfo[MESSAGE] = err.message;
  36. // Assign the stack and/or cause if requested.
  37. if (stack) einfo.stack = err.stack;
  38. if (cause) einfo.cause = err.cause;
  39. return einfo;
  40. });