constructor.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Ono = void 0;
  4. const extend_error_1 = require("./extend-error");
  5. const normalize_1 = require("./normalize");
  6. const to_json_1 = require("./to-json");
  7. const constructor = Ono;
  8. exports.Ono = constructor;
  9. /**
  10. * Creates an `Ono` instance for a specifc error type.
  11. */
  12. // eslint-disable-next-line @typescript-eslint/naming-convention
  13. function Ono(ErrorConstructor, options) {
  14. options = normalize_1.normalizeOptions(options);
  15. function ono(...args) {
  16. let { originalError, props, message } = normalize_1.normalizeArgs(args, options);
  17. // Create a new error of the specified type
  18. let newError = new ErrorConstructor(message);
  19. // Extend the error with the properties of the original error and the `props` object
  20. return extend_error_1.extendError(newError, originalError, props);
  21. }
  22. ono[Symbol.species] = ErrorConstructor;
  23. return ono;
  24. }
  25. /**
  26. * Returns an object containing all properties of the given Error object,
  27. * which can be used with `JSON.stringify()`.
  28. */
  29. Ono.toJSON = function toJSON(error) {
  30. return to_json_1.toJSON.call(error);
  31. };
  32. /**
  33. * Extends the given Error object with enhanced Ono functionality, such as nested stack traces,
  34. * additional properties, and improved support for `JSON.stringify()`.
  35. */
  36. Ono.extend = function extend(error, originalError, props) {
  37. if (props || originalError instanceof Error) {
  38. return extend_error_1.extendError(error, originalError, props);
  39. }
  40. else if (originalError) {
  41. return extend_error_1.extendError(error, undefined, originalError);
  42. }
  43. else {
  44. return extend_error_1.extendError(error);
  45. }
  46. };
  47. //# sourceMappingURL=constructor.js.map