您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict';
  2. function assign(obj, props) {
  3. for (const key in props) {
  4. Object.defineProperty(obj, key, {
  5. value: props[key],
  6. enumerable: true,
  7. configurable: true,
  8. });
  9. }
  10. return obj;
  11. }
  12. function createError(err, code, props) {
  13. if (!err || typeof err === 'string') {
  14. throw new TypeError('Please pass an Error to err-code');
  15. }
  16. if (!props) {
  17. props = {};
  18. }
  19. if (typeof code === 'object') {
  20. props = code;
  21. code = undefined;
  22. }
  23. if (code != null) {
  24. props.code = code;
  25. }
  26. try {
  27. return assign(err, props);
  28. } catch (_) {
  29. props.message = err.message;
  30. props.stack = err.stack;
  31. const ErrClass = function () {};
  32. ErrClass.prototype = Object.create(Object.getPrototypeOf(err));
  33. return assign(new ErrClass(), props);
  34. }
  35. }
  36. module.exports = createError;