@api-client/core 0.19.15 → 0.19.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"XmlReader.d.ts","sourceRoot":"","sources":["../../../src/data/XmlReader.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAI5C;;;GAGG;AACH,qBAAa,SAAU,SAAQ,UAAU;IACjC,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAQ9C,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,QAAQ,GAAG,OAAO;IAuBnD,iBAAiB,CAAC,MAAM,EAAE,WAAW,GAAG,OAAO;IA8BzC,WAAW,IAAI,OAAO,CAAC,QAAQ,GAAG,SAAS,CAAC;IAQ5C,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,GAAG,SAAS,CAAC;IAOhE,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,GAAG,SAAS,CAAC;IAS3D,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,GAAG,SAAS,CAAC;CA+BnE"}
1
+ {"version":3,"file":"XmlReader.d.ts","sourceRoot":"","sources":["../../../src/data/XmlReader.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAI5C;;;GAGG;AACH,qBAAa,SAAU,SAAQ,UAAU;IACjC,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAQ9C,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,QAAQ,GAAG,OAAO;IAuBnD,iBAAiB,CAAC,MAAM,EAAE,WAAW,GAAG,OAAO;IA8BzC,WAAW,IAAI,OAAO,CAAC,QAAQ,GAAG,SAAS,CAAC;IAQ5C,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,GAAG,SAAS,CAAC;IAOhE,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,GAAG,SAAS,CAAC;IAS3D,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,GAAG,SAAS,CAAC;CAgCnE"}
@@ -92,7 +92,8 @@ export class XmlReader extends DataReader {
92
92
  xResult = xpath.default.XPathResult;
93
93
  let errored = false;
94
94
  const parser = new DOMParser({
95
- errorHandler: () => {
95
+ onError: (level, msg) => {
96
+ console.error(`[${level}] ${msg}`);
96
97
  errored = true;
97
98
  },
98
99
  });
@@ -100,8 +101,8 @@ export class XmlReader extends DataReader {
100
101
  try {
101
102
  dom = parser.parseFromString(data, 'text/xml');
102
103
  }
103
- catch {
104
- //
104
+ catch (e) {
105
+ console.error(e);
105
106
  }
106
107
  if (!dom || errored) {
107
108
  return undefined;
@@ -1 +1 @@
1
- {"version":3,"file":"XmlReader.js","sourceRoot":"","sources":["../../../src/data/XmlReader.ts"],"names":[],"mappings":"AAAA,+BAA+B;AAC/B,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAE5C,IAAI,OAAO,GAAmC,OAAO,WAAW,KAAK,WAAW,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,SAAS,CAAA;AAE1G;;;GAGG;AACH,MAAM,OAAO,SAAU,SAAQ,UAAU;IACvC,KAAK,CAAC,QAAQ,CAAC,IAAY;QACzB,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,WAAW,EAAE,CAAA;QACpC,IAAI,CAAC,GAAG,EAAE,CAAC;YACT,OAAO,SAAS,CAAA;QAClB,CAAC;QACD,OAAO,IAAI,CAAC,aAAa,CAAC,IAAI,EAAE,GAAG,CAAC,CAAA;IACtC,CAAC;IAED,aAAa,CAAC,IAAY,EAAE,GAAa;QACvC,IAAI,CAAC;Y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AC;IAED,KAAK,CAAC,eAAe,CAAC,IAAY;QAChC,MAAM,EAAE,SAAS,EAAE,GAAG,MAAM,MAAM,CAAC,gBAAgB,CAAC,CAAA;QACpD,MAAM,KAAK,GAAG,MAAM,MAAM,CAAC,OAAO,CAAC,CAAA;QACnC,wDAAwD;QACxD,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC,WAAW,CAAA;QACnC,IAAI,OAAO,GAAG,KAAK,CAAA;QACnB,MAAM,MAAM,GAAG,IAAI,SAAS,CAAC;YAC3B,YAAY,EAAE,GAAS,EAAE;gBACvB,OAAO,GAAG,IAAI,CAAA;YAChB,CAAC;SACF,CAAC,CAAA;QACF,IAAI,GAAyB,CAAA;QAC7B,IAAI,CAAC;YACH,GAAG,GAAG,MAAM,CAAC,eAAe,CAAC,IAAI,EAAE,UAAU,CAAwB,CAAA;QACvE,CAAC;QAAC,MAAM,CAAC;YACP,EAAE;QACJ,CAAC;QACD,IAAI,CAAC,GAAG,IAAI,OAAO,EAAE,CAAC;YACpB,OAAO,SAAS,CAAA;QAClB,CAAC;QACD,MAAM,CAAC,cAAc,CAAC,GAAG,EAAE,UAAU,EAAE;YACrC,KAAK,EAAE,UAAU,UAAkB,EAAE,WAAiB;gBACpD,uDAAuD;gBACvD,OAAO,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,UAAU,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,CAAC,CAAA;YACvE,CAAC;YACD,UAAU,EAAE,IAAI,EAAE,mCAAmC;YACrD,YAAY,EAAE,IAAI,EAAE,8CAA8C;YAClE,QAAQ,EAAE,IAAI,EAAE,yCAAyC;SAC1D,CAAC,CAAA;QACF,OAAO,GAA0B,CAAA;IACnC,CAAC;CACF","sourcesContent":["/* eslint-disable no-console */\nimport { DataReader } from './DataReader.js'\n\nlet xResult: typeof XPathResult | undefined = typeof XPathResult !== 'undefined' ? XPathResult : undefined\n\n/**\n * Reads the data from an XML using the xpath.\n * See https://www.w3schools.com/xml/xpath_syntax.asp.\n */\nexport class XmlReader extends DataReader {\n async getValue(path: string): Promise<unknown> {\n const doc = await this.getDocument()\n if (!doc) {\n return undefined\n }\n return this.queryDocument(path, doc)\n }\n\n queryDocument(path: string, doc: Document): unknown {\n try {\n const result = doc.evaluate(path, doc)\n switch (result.resultType) {\n case (xResult as typeof XPathResult).NUMBER_TYPE:\n return result.numberValue\n case (xResult as typeof XPathResult).STRING_TYPE:\n return result.stringValue\n case (xResult as typeof XPathResult).BOOLEAN_TYPE:\n return result.booleanValue\n case (xResult as typeof XPathResult).UNORDERED_NODE_ITERATOR_TYPE:\n case (xResult as typeof XPathResult).ORDERED_NODE_ITERATOR_TYPE:\n return this.processNodeResult(result)\n default:\n console.error(`Unsupported xpath result type: [${result.resultType}]`)\n return undefined\n }\n } catch (e) {\n console.error(e)\n return undefined\n }\n }\n\n processNodeResult(result: XPathResult): unknown {\n const results: unknown[] = []\n let next = result.iterateNext()\n while (next) {\n const value = next.textContent\n if (value) {\n results.push(value.trim())\n }\n next = result.iterateNext()\n }\n if (!results.length) {\n return undefined\n }\n\n const mapped = results.map((item) => {\n if (item === 'true') {\n return true\n }\n if (item === 'true') {\n return false\n }\n const typedNumber = Number(item)\n if (!Number.isNaN(typedNumber)) {\n return typedNumber\n }\n return item\n })\n return mapped.length === 1 ? mapped[0] : mapped\n }\n\n async getDocument(): Promise<Document | undefined> {\n const body = await this.readPayloadAsString()\n if (!body) {\n return undefined\n }\n return this.getDocumentWithBody(body)\n }\n\n async getDocumentWithBody(body: string): Promise<Document | undefined> {\n if (typeof DOMParser === 'undefined') {\n return this.getNodeDocument(body)\n }\n return this.getWebDocument(body)\n }\n\n async getWebDocument(data: string): Promise<Document | undefined> {\n const parser = new DOMParser()\n const dom = parser.parseFromString(data, 'text/xml')\n if (dom.querySelector('parsererror')) {\n return undefined\n }\n return dom\n }\n\n async getNodeDocument(data: string): Promise<Document | undefined> {\n const { DOMParser } = await import('@xmldom/xmldom')\n const xpath = await import('xpath')\n // @ts-expect-error The xpath has invalid types defined.\n xResult = xpath.default.XPathResult\n let errored = false\n const parser = new DOMParser({\n errorHandler: (): void => {\n errored = true\n },\n })\n let dom: Document | undefined\n try {\n dom = parser.parseFromString(data, 'text/xml') as unknown as Document\n } catch {\n //\n }\n if (!dom || errored) {\n return undefined\n }\n Object.defineProperty(dom, 'evaluate', {\n value: function (expression: string, contextNode: Node) {\n // @ts-expect-error The xpath library has invalid types\n return xpath.default.evaluate(expression, contextNode, null, 0, null)\n },\n enumerable: true, // Make the method visible in loops\n configurable: true, // Allow the method to be deleted or redefined\n writable: true, // Allow the method's value to be changed\n })\n return dom as unknown as Document\n }\n}\n"]}
1
+ {"version":3,"file":"XmlReader.js","sourceRoot":"","sources":["../../../src/data/XmlReader.ts"],"names":[],"mappings":"AAAA,+BAA+B;AAC/B,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAE5C,IAAI,OAAO,GAAmC,OAAO,WAAW,KAAK,WAAW,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,SAAS,CAAA;AAE1G;;;GAGG;AACH,MAAM,OAAO,SAAU,SAAQ,UAAU;IACvC,KAAK,CAAC,QAAQ,CAAC,IAAY;QACzB,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,WAAW,EAAE,CAAA;QACpC,IAAI,CAAC,GAAG,EAAE,CAAC;YACT,OAAO,SAAS,CAAA;QAClB,CAAC;QACD,OAAO,IAAI,CAAC,aAAa,CAAC,IAAI,EAAE,GAAG,CAAC,CAAA;IACtC,CAAC;IAED,aAAa,CAAC,IAAY,EAAE,GAAa;QACvC,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,GAAG,CAAC,QAAQ,CAAC,IAAI,EAAE,GAAG,CAAC,CAAA;YACtC,QAAQ,MAAM,CAAC,UAAU,EAAE,CAAC;gBAC1B,KAAM,OAA8B,CAAC,WAAW;oBAC9C,OAAO,MAAM,CAAC,WAAW,CAAA;gBAC3B,KAAM,OAA8B,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AC,OAAO,CAAC,WAAW,CAAA;QACnC,IAAI,OAAO,GAAG,KAAK,CAAA;QACnB,MAAM,MAAM,GAAG,IAAI,SAAS,CAAC;YAC3B,OAAO,EAAE,CAAC,KAAK,EAAE,GAAG,EAAQ,EAAE;gBAC5B,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,KAAK,GAAG,EAAE,CAAC,CAAA;gBAClC,OAAO,GAAG,IAAI,CAAA;YAChB,CAAC;SACF,CAAC,CAAA;QACF,IAAI,GAAyB,CAAA;QAC7B,IAAI,CAAC;YACH,GAAG,GAAG,MAAM,CAAC,eAAe,CAAC,IAAI,EAAE,UAAU,CAAwB,CAAA;QACvE,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;QAClB,CAAC;QACD,IAAI,CAAC,GAAG,IAAI,OAAO,EAAE,CAAC;YACpB,OAAO,SAAS,CAAA;QAClB,CAAC;QACD,MAAM,CAAC,cAAc,CAAC,GAAG,EAAE,UAAU,EAAE;YACrC,KAAK,EAAE,UAAU,UAAkB,EAAE,WAAiB;gBACpD,uDAAuD;gBACvD,OAAO,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,UAAU,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,CAAC,CAAA;YACvE,CAAC;YACD,UAAU,EAAE,IAAI,EAAE,mCAAmC;YACrD,YAAY,EAAE,IAAI,EAAE,8CAA8C;YAClE,QAAQ,EAAE,IAAI,EAAE,yCAAyC;SAC1D,CAAC,CAAA;QACF,OAAO,GAA0B,CAAA;IACnC,CAAC;CACF","sourcesContent":["/* eslint-disable no-console */\nimport { DataReader } from './DataReader.js'\n\nlet xResult: typeof XPathResult | undefined = typeof XPathResult !== 'undefined' ? XPathResult : undefined\n\n/**\n * Reads the data from an XML using the xpath.\n * See https://www.w3schools.com/xml/xpath_syntax.asp.\n */\nexport class XmlReader extends DataReader {\n async getValue(path: string): Promise<unknown> {\n const doc = await this.getDocument()\n if (!doc) {\n return undefined\n }\n return this.queryDocument(path, doc)\n }\n\n queryDocument(path: string, doc: Document): unknown {\n try {\n const result = doc.evaluate(path, doc)\n switch (result.resultType) {\n case (xResult as typeof XPathResult).NUMBER_TYPE:\n return result.numberValue\n case (xResult as typeof XPathResult).STRING_TYPE:\n return result.stringValue\n case (xResult as typeof XPathResult).BOOLEAN_TYPE:\n return result.booleanValue\n case (xResult as typeof XPathResult).UNORDERED_NODE_ITERATOR_TYPE:\n case (xResult as typeof XPathResult).ORDERED_NODE_ITERATOR_TYPE:\n return this.processNodeResult(result)\n default:\n console.error(`Unsupported xpath result type: [${result.resultType}]`)\n return undefined\n }\n } catch (e) {\n console.error(e)\n return undefined\n }\n }\n\n processNodeResult(result: XPathResult): unknown {\n const results: unknown[] = []\n let next = result.iterateNext()\n while (next) {\n const value = next.textContent\n if (value) {\n results.push(value.trim())\n }\n next = result.iterateNext()\n }\n if (!results.length) {\n return undefined\n }\n\n const mapped = results.map((item) => {\n if (item === 'true') {\n return true\n }\n if (item === 'true') {\n return false\n }\n const typedNumber = Number(item)\n if (!Number.isNaN(typedNumber)) {\n return typedNumber\n }\n return item\n })\n return mapped.length === 1 ? mapped[0] : mapped\n }\n\n async getDocument(): Promise<Document | undefined> {\n const body = await this.readPayloadAsString()\n if (!body) {\n return undefined\n }\n return this.getDocumentWithBody(body)\n }\n\n async getDocumentWithBody(body: string): Promise<Document | undefined> {\n if (typeof DOMParser === 'undefined') {\n return this.getNodeDocument(body)\n }\n return this.getWebDocument(body)\n }\n\n async getWebDocument(data: string): Promise<Document | undefined> {\n const parser = new DOMParser()\n const dom = parser.parseFromString(data, 'text/xml')\n if (dom.querySelector('parsererror')) {\n return undefined\n }\n return dom\n }\n\n async getNodeDocument(data: string): Promise<Document | undefined> {\n const { DOMParser } = await import('@xmldom/xmldom')\n const xpath = await import('xpath')\n // @ts-expect-error The xpath has invalid types defined.\n xResult = xpath.default.XPathResult\n let errored = false\n const parser = new DOMParser({\n onError: (level, msg): void => {\n console.error(`[${level}] ${msg}`)\n errored = true\n },\n })\n let dom: Document | undefined\n try {\n dom = parser.parseFromString(data, 'text/xml') as unknown as Document\n } catch (e) {\n console.error(e)\n }\n if (!dom || errored) {\n return undefined\n }\n Object.defineProperty(dom, 'evaluate', {\n value: function (expression: string, contextNode: Node) {\n // @ts-expect-error The xpath library has invalid types\n return xpath.default.evaluate(expression, contextNode, null, 0, null)\n },\n enumerable: true, // Make the method visible in loops\n configurable: true, // Allow the method to be deleted or redefined\n writable: true, // Allow the method's value to be changed\n })\n return dom as unknown as Document\n }\n}\n"]}
@@ -1,3 +1,8 @@
1
+ /**
2
+ * A serialized representation of an Exception.
3
+ * Provides a standardized structure for pure data objects,
4
+ * typically used when converting errors for JSON responses.
5
+ */
1
6
  export interface ExceptionSchema {
2
7
  name: string;
3
8
  message: string;
@@ -68,16 +73,67 @@ export declare class Exception extends Error {
68
73
  * ```
69
74
  */
70
75
  static fromRawException(init: object, defaultMessage: string): Exception;
76
+ /**
77
+ * Initializes a new Exception instance.
78
+ *
79
+ * @param message - The primary human-readable error message.
80
+ * @param options - Additional properties configuration. Accepts standard `ErrorOptions`
81
+ * (like `cause`), plus custom `code`, `status`, and `help`.
82
+ */
71
83
  constructor(message?: string, options?: ErrorOptions & {
72
84
  code?: string;
73
85
  status?: number;
74
86
  help?: string;
75
87
  });
88
+ /**
89
+ * Assigns a human-readable help description for troubleshooting and returns
90
+ * the exception instance to allow for method chaining.
91
+ *
92
+ * @param help - The detailed help informational text.
93
+ * @returns The current exception instance.
94
+ */
76
95
  setHelp(help: string): this;
96
+ /**
97
+ * Assigns a programmatic error code and returns the exception instance
98
+ * to allow for method chaining.
99
+ *
100
+ * @param code - The string error code (e.g., 'E_FILE_NOT_FOUND').
101
+ * @returns The current exception instance.
102
+ */
77
103
  setCode(code: string): this;
104
+ /**
105
+ * Assigns an HTTP-style status code and returns the exception instance
106
+ * to allow for method chaining.
107
+ *
108
+ * @param status - The numeric status code (e.g., 404, 500).
109
+ * @returns The current exception instance.
110
+ */
78
111
  setStatus(status: number): this;
112
+ /**
113
+ * Provides the string tag used by `Object.prototype.toString`.
114
+ */
79
115
  get [Symbol.toStringTag](): string;
116
+ /**
117
+ * Computes a string representation of the exception, including the
118
+ * programmatic code if one is defined.
119
+ */
80
120
  toString(): string;
121
+ /**
122
+ * Serializes the exception into a plain JavaScript object.
123
+ * This ensures safe passing across boundaries and correct format in JSON responses.
124
+ *
125
+ * @returns The serialized exception object matching `ExceptionSchema`.
126
+ */
81
127
  toJSON(): ExceptionSchema;
82
128
  }
129
+ /**
130
+ * Converts a standard Error object (or thrown primitive) into an Exception instance.
131
+ * This function extracts relevant properties from the raw error
132
+ * and sets them on the Exception instance, allowing for a more
133
+ * consistent error handling experience across the application.
134
+ * @param error The unknown error value to convert.
135
+ * @param defaults Optional default properties to apply if they are missing on the Error object.
136
+ * @returns An Exception instance with properties set from the Error object.
137
+ */
138
+ export declare function fromError(error: unknown, defaults?: Partial<ExceptionSchema>): Exception;
83
139
  //# sourceMappingURL=exception.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"exception.d.ts","sourceRoot":"","sources":["../../../src/exceptions/exception.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,MAAM,CAAA;IACf,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,CAAA;CAC3C;AAED;;;;;;;;;;;;;;GAcG;AACH,qBAAa,SAAU,SAAQ,KAAK;IAClC;;;OAGG;IACH,OAAe,IAAI,CAAC,EAAE,MAAM,CAAA;IAC5B,OAAe,IAAI,CAAC,EAAE,MAAM,CAAA;IAC5B,OA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Exception extends Error {
43
43
  static fromRawException(init, defaultMessage) {
44
44
  const typed = init;
45
45
  const options = {};
46
- if (typed.code) {
47
- options.code = typed.code;
46
+ if (typed.code !== undefined) {
47
+ options.code = String(typed.code);
48
48
  }
49
- if (typed.status) {
50
- options.status = Number(typed.status);
49
+ if (typed.status !== undefined) {
50
+ const parsedStatus = Number(typed.status);
51
+ if (!isNaN(parsedStatus)) {
52
+ options.status = parsedStatus;
53
+ }
51
54
  }
52
- const message = typed.message ?? defaultMessage;
53
- const result = new this(message, options);
54
- if (typed.help) {
55
- result.setHelp(typed.help);
55
+ if (typed.help !== undefined) {
56
+ options.help = String(typed.help);
56
57
  }
57
- if (typed.name) {
58
- result.name = typed.name;
58
+ const message = typed.message !== undefined ? String(typed.message) : defaultMessage;
59
+ const result = new this(message, options);
60
+ if (typed.name !== undefined) {
61
+ result.name = String(typed.name);
59
62
  }
60
63
  return result;
61
64
  }
65
+ /**
66
+ * Initializes a new Exception instance.
67
+ *
68
+ * @param message - The primary human-readable error message.
69
+ * @param options - Additional properties configuration. Accepts standard `ErrorOptions`
70
+ * (like `cause`), plus custom `code`, `status`, and `help`.
71
+ */
62
72
  constructor(message, options) {
63
73
  super(message, options);
64
74
  const ErrorConstructor = this.constructor;
65
75
  this.name = ErrorConstructor.name;
66
- this.message = message || ErrorConstructor.message || '';
67
- this.status = options?.status || ErrorConstructor.status || 500;
68
- const code = options?.code || ErrorConstructor.code;
76
+ this.message = message ?? ErrorConstructor.message ?? '';
77
+ this.status = options?.status ?? ErrorConstructor.status ?? 500;
78
+ const code = options?.code ?? ErrorConstructor.code;
69
79
  if (code !== undefined) {
70
80
  this.code = code;
71
81
  }
72
- const help = options?.help || ErrorConstructor.help;
82
+ const help = options?.help ?? ErrorConstructor.help;
73
83
  if (help !== undefined) {
74
84
  this.help = help;
75
85
  }
76
86
  Error.captureStackTrace(this, ErrorConstructor);
77
87
  }
88
+ /**
89
+ * Assigns a human-readable help description for troubleshooting and returns
90
+ * the exception instance to allow for method chaining.
91
+ *
92
+ * @param help - The detailed help informational text.
93
+ * @returns The current exception instance.
94
+ */
78
95
  setHelp(help) {
79
96
  this.help = help;
80
97
  return this;
81
98
  }
99
+ /**
100
+ * Assigns a programmatic error code and returns the exception instance
101
+ * to allow for method chaining.
102
+ *
103
+ * @param code - The string error code (e.g., 'E_FILE_NOT_FOUND').
104
+ * @returns The current exception instance.
105
+ */
82
106
  setCode(code) {
83
107
  this.code = code;
84
108
  return this;
85
109
  }
110
+ /**
111
+ * Assigns an HTTP-style status code and returns the exception instance
112
+ * to allow for method chaining.
113
+ *
114
+ * @param status - The numeric status code (e.g., 404, 500).
115
+ * @returns The current exception instance.
116
+ */
86
117
  setStatus(status) {
87
118
  this.status = status;
88
119
  return this;
89
120
  }
121
+ /**
122
+ * Provides the string tag used by `Object.prototype.toString`.
123
+ */
90
124
  get [Symbol.toStringTag]() {
91
125
  return this.constructor.name;
92
126
  }
127
+ /**
128
+ * Computes a string representation of the exception, including the
129
+ * programmatic code if one is defined.
130
+ */
93
131
  toString() {
94
132
  if (this.code) {
95
133
  return `${this.name} [${this.code}]: ${this.message}`;
96
134
  }
97
135
  return `${this.name}: ${this.message}`;
98
136
  }
137
+ /**
138
+ * Serializes the exception into a plain JavaScript object.
139
+ * This ensures safe passing across boundaries and correct format in JSON responses.
140
+ *
141
+ * @returns The serialized exception object matching `ExceptionSchema`.
142
+ */
99
143
  toJSON() {
100
144
  const result = {
101
145
  name: this.name,
@@ -113,4 +157,57 @@ export class Exception extends Error {
113
157
  return result;
114
158
  }
115
159
  }
160
+ /**
161
+ * Converts a standard Error object (or thrown primitive) into an Exception instance.
162
+ * This function extracts relevant properties from the raw error
163
+ * and sets them on the Exception instance, allowing for a more
164
+ * consistent error handling experience across the application.
165
+ * @param error The unknown error value to convert.
166
+ * @param defaults Optional default properties to apply if they are missing on the Error object.
167
+ * @returns An Exception instance with properties set from the Error object.
168
+ */
169
+ export function fromError(error, defaults = {}) {
170
+ let message;
171
+ let typed = {};
172
+ if (error instanceof Error) {
173
+ message = error.message;
174
+ typed = error;
175
+ }
176
+ else if (typeof error === 'string') {
177
+ message = error;
178
+ }
179
+ else if (error !== null && typeof error === 'object') {
180
+ typed = error;
181
+ if (typeof typed.message === 'string') {
182
+ message = typed.message;
183
+ }
184
+ }
185
+ const e = new Exception(message || defaults.message || 'An error occurred');
186
+ if (typed.code !== undefined) {
187
+ e.code = String(typed.code);
188
+ }
189
+ else if (defaults.code !== undefined) {
190
+ e.code = defaults.code;
191
+ }
192
+ if (typed.status !== undefined) {
193
+ const status = Number(typed.status);
194
+ e.status = isNaN(status) ? (defaults.status ?? 500) : status;
195
+ }
196
+ else if (defaults.status !== undefined) {
197
+ e.status = defaults.status;
198
+ }
199
+ if (typed.help !== undefined) {
200
+ e.setHelp(String(typed.help));
201
+ }
202
+ else if (defaults.help !== undefined) {
203
+ e.setHelp(defaults.help);
204
+ }
205
+ if (typed.name !== undefined) {
206
+ e.name = String(typed.name);
207
+ }
208
+ else if (defaults.name !== undefined) {
209
+ e.name = defaults.name;
210
+ }
211
+ return e;
212
+ }
116
213
  //# sourceMappingURL=exception.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"exception.js","sourceRoot":"","sources":["../../../src/exceptions/exception.ts"],"names":[],"mappings":"AAUA;;;;;;;;;;;;;;GAcG;AACH,MAAM,OAAO,SAAU,SAAQ,KAAK;IAUlC;;OAEG;IACM,IAAI,CAAQ;IAcrB;;;OAGG;IACH,MAAM,CAAQ;IAEd;;;;;;;;;;;;;;;;OAgBG;IACH,MAAM,CAAC,gBAAgB,CAAC,IAAY,EAAE,cAAsB;QAC1D,MAAM,KAAK,GAAG,IAAuC,CAAA;QACrD,MAAM,OAAO,GAAsD,EAAE,CAAA;QACrE,IAAI,KAAK,CAAC,IAAI,EAAE,CAAC;YACf,OAAO,CAAC,IAAI,GAAG,KAAK,CAAC,IAAc,CAAA;QACrC,CAAC;QACD,IAAI,KAAK,CAAC,MAAM,EAAE,CAAC;YACjB,OAA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jD,CAAC;IAED,OAAO,CAAC,IAAY;QAClB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,OAAO,IAAI,CAAA;IACb,CAAC;IAED,OAAO,CAAC,IAAY;QAClB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,OAAO,IAAI,CAAA;IACb,CAAC;IAED,SAAS,CAAC,MAAc;QACtB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,OAAO,IAAI,CAAA;IACb,CAAC;IAED,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC;QACtB,OAAO,IAAI,CAAC,WAAW,CAAC,IAAI,CAAA;IAC9B,CAAC;IAEQ,QAAQ;QACf,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;YACd,OAAO,GAAG,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,MAAM,IAAI,CAAC,OAAO,EAAE,CAAA;QACvD,CAAC;QACD,OAAO,GAAG,IAAI,CAAC,IAAI,KAAK,IAAI,CAAC,OAAO,EAAE,CAAA;IACxC,CAAC;IAED,MAAM;QACJ,MAAM,MAAM,GAAoB;YAC9B,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,OAAO,EAAE,IAAI,CAAC,OAAO;SACtB,CAAA;QACD,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;YAChB,MAAM,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAA;QAC7B,CAAC;QACD,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;YACd,MAAM,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAA;QACzB,CAAC;QACD,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;YACd,MAAM,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAA;QACzB,CAAC;QACD,OAAO,MAAM,CAAA;IACf,CAAC;CACF","sourcesContent":["export interface ExceptionSchema {\n name: string\n message: string\n status?: number\n code?: string\n help?: string\n stack?: string\n [key: string]: string | number | undefined\n}\n\n/**\n * Create a custom error class with the ability to define the error\n * code, status, and the help text.\n *\n * ```js\n * export class FileNotFoundException extends Exception {\n * static status = 500\n * static code = 'E_FILE_NOT_FOUND'\n * }\n *\n * throw new FileNotFoundException(\n * `Unable to find file from ${filePath} location`\n * )\n * ```\n */\nexport class Exception extends Error {\n /**\n * Define the error metadata as static properties to avoid\n * setting them repeatedly on the error instance\n */\n declare static help?: string\n declare static code?: string\n declare static status?: number\n declare static message?: string\n\n /**\n * Name of the class that raised the exception.\n */\n override name: string\n\n /**\n * Optional help description for the error. You can use it to define additional\n * human readable information for the error.\n */\n declare help?: string\n\n /**\n * A machine readable error code. This will allow the error handling logic\n * to narrow down exceptions based upon the error code.\n */\n declare code?: string\n\n /**\n * A status code for the error. Usually helpful when converting errors\n * to HTTP responses.\n */\n status: number\n\n /**\n * Create a new exception instance.\n * It tries to extract the error code and status from the\n * `init` object. If not found, it uses the default message.\n * @param init - An object containing error details\n * @param defaultMessage - A default message to use if `init.message` is not provided\n * @returns An instance of the Exception class\n * @example\n * ```js\n * const error = Exception.fromApiError({\n * code: 'E_FILE_NOT_FOUND',\n * status: 404,\n * message: 'File not found',\n * help: 'Please check the file path and try again'\n * }, 'An error occurred while processing your request')\n * ```\n */\n static fromRawException(init: object, defaultMessage: string): Exception {\n const typed = init as Record<string, string | number>\n const options: ErrorOptions & { code?: string; status?: number } = {}\n if (typed.code) {\n options.code = typed.code as string\n }\n if (typed.status) {\n options.status = Number(typed.status)\n }\n const message = typed.message ?? defaultMessage\n const result = new this(message as string, options)\n if (typed.help) {\n result.setHelp(typed.help as string)\n }\n if (typed.name) {\n result.name = typed.name as string\n }\n return result\n }\n\n constructor(message?: string, options?: ErrorOptions & { code?: string; status?: number; help?: string }) {\n super(message, options)\n\n const ErrorConstructor = this.constructor as typeof Exception\n\n this.name = ErrorConstructor.name\n this.message = message || ErrorConstructor.message || ''\n this.status = options?.status || ErrorConstructor.status || 500\n\n const code = options?.code || ErrorConstructor.code\n if (code !== undefined) {\n this.code = code\n }\n\n const help = options?.help || ErrorConstructor.help\n if (help !== undefined) {\n this.help = help\n }\n\n Error.captureStackTrace(this, ErrorConstructor)\n }\n\n setHelp(help: string): this {\n this.help = help\n return this\n }\n\n setCode(code: string): this {\n this.code = code\n return this\n }\n\n setStatus(status: number): this {\n this.status = status\n return this\n }\n\n get [Symbol.toStringTag]() {\n return this.constructor.name\n }\n\n override toString(): string {\n if (this.code) {\n return `${this.name} [${this.code}]: ${this.message}`\n }\n return `${this.name}: ${this.message}`\n }\n\n toJSON(): ExceptionSchema {\n const result: ExceptionSchema = {\n name: this.name,\n message: this.message,\n }\n if (this.status) {\n result.status = this.status\n }\n if (this.code) {\n result.code = this.code\n }\n if (this.help) {\n result.help = this.help\n }\n return result\n }\n}\n"]}
1
+ {"version":3,"file":"exception.js","sourceRoot":"","sources":["../../../src/exceptions/exception.ts"],"names":[],"mappings":"AAeA;;;;;;;;;;;;;;GAcG;AACH,MAAM,OAAO,SAAU,SAAQ,KAAK;IAUlC;;OAEG;IACM,IAAI,CAAQ;IAcrB;;;OAGG;IACH,MAAM,CAAQ;IAEd;;;;;;;;;;;;;;;;OAgBG;IACH,MAAM,CAAC,gBAAgB,CAAC,IAAY,EAAE,cAAsB;QAC1D,MAAM,KAAK,GAAG,IAAuC,CAAA;QACrD,MAAM,OAAO,GAAqE,EAAE,CAAA;QACpF,IAAI,KAAK,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;YAC7B,OAAO,CAAC,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;QACnC,CAAC;QACD,IAAI,KAAK,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;YAC/B,MAAM,YAAY,GAAG,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,CAAA;YACzC,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,EAAE,CAAC;gBACzB,OAAO,CAAC,MAAM,GAAG,YAAY,CAAA;YAC/B,CAAC;QACH,CAAC;QACD,IAAI,KAAK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,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAA;IAC9D,CAAC;SAAM,IAAI,QAAQ,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;QACzC,CAAC,CAAC,MAAM,GAAG,QAAQ,CAAC,MAAM,CAAA;IAC5B,CAAC;IAED,IAAI,KAAK,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;QAC7B,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAA;IAC/B,CAAC;SAAM,IAAI,QAAQ,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;QACvC,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAA;IAC1B,CAAC;IAED,IAAI,KAAK,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;QAC7B,CAAC,CAAC,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;IAC7B,CAAC;SAAM,IAAI,QAAQ,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;QACvC,CAAC,CAAC,IAAI,GAAG,QAAQ,CAAC,IAAI,CAAA;IACxB,CAAC;IAED,OAAO,CAAC,CAAA;AACV,CAAC","sourcesContent":["/**\n * A serialized representation of an Exception.\n * Provides a standardized structure for pure data objects,\n * typically used when converting errors for JSON responses.\n */\nexport interface ExceptionSchema {\n name: string\n message: string\n status?: number\n code?: string\n help?: string\n stack?: string\n [key: string]: string | number | undefined\n}\n\n/**\n * Create a custom error class with the ability to define the error\n * code, status, and the help text.\n *\n * ```js\n * export class FileNotFoundException extends Exception {\n * static status = 500\n * static code = 'E_FILE_NOT_FOUND'\n * }\n *\n * throw new FileNotFoundException(\n * `Unable to find file from ${filePath} location`\n * )\n * ```\n */\nexport class Exception extends Error {\n /**\n * Define the error metadata as static properties to avoid\n * setting them repeatedly on the error instance\n */\n declare static help?: string\n declare static code?: string\n declare static status?: number\n declare static message?: string\n\n /**\n * Name of the class that raised the exception.\n */\n override name: string\n\n /**\n * Optional help description for the error. You can use it to define additional\n * human readable information for the error.\n */\n declare help?: string\n\n /**\n * A machine readable error code. This will allow the error handling logic\n * to narrow down exceptions based upon the error code.\n */\n declare code?: string\n\n /**\n * A status code for the error. Usually helpful when converting errors\n * to HTTP responses.\n */\n status: number\n\n /**\n * Create a new exception instance.\n * It tries to extract the error code and status from the\n * `init` object. If not found, it uses the default message.\n * @param init - An object containing error details\n * @param defaultMessage - A default message to use if `init.message` is not provided\n * @returns An instance of the Exception class\n * @example\n * ```js\n * const error = Exception.fromApiError({\n * code: 'E_FILE_NOT_FOUND',\n * status: 404,\n * message: 'File not found',\n * help: 'Please check the file path and try again'\n * }, 'An error occurred while processing your request')\n * ```\n */\n static fromRawException(init: object, defaultMessage: string): Exception {\n const typed = init as Record<string, string | number>\n const options: ErrorOptions & { code?: string; status?: number; help?: string } = {}\n if (typed.code !== undefined) {\n options.code = String(typed.code)\n }\n if (typed.status !== undefined) {\n const parsedStatus = Number(typed.status)\n if (!isNaN(parsedStatus)) {\n options.status = parsedStatus\n }\n }\n if (typed.help !== undefined) {\n options.help = String(typed.help)\n }\n const message = typed.message !== undefined ? String(typed.message) : defaultMessage\n const result = new this(message, options)\n if (typed.name !== undefined) {\n result.name = String(typed.name)\n }\n return result\n }\n\n /**\n * Initializes a new Exception instance.\n *\n * @param message - The primary human-readable error message.\n * @param options - Additional properties configuration. Accepts standard `ErrorOptions`\n * (like `cause`), plus custom `code`, `status`, and `help`.\n */\n constructor(message?: string, options?: ErrorOptions & { code?: string; status?: number; help?: string }) {\n super(message, options)\n\n const ErrorConstructor = this.constructor as typeof Exception\n\n this.name = ErrorConstructor.name\n this.message = message ?? ErrorConstructor.message ?? ''\n this.status = options?.status ?? ErrorConstructor.status ?? 500\n\n const code = options?.code ?? ErrorConstructor.code\n if (code !== undefined) {\n this.code = code\n }\n\n const help = options?.help ?? ErrorConstructor.help\n if (help !== undefined) {\n this.help = help\n }\n\n Error.captureStackTrace(this, ErrorConstructor)\n }\n\n /**\n * Assigns a human-readable help description for troubleshooting and returns\n * the exception instance to allow for method chaining.\n *\n * @param help - The detailed help informational text.\n * @returns The current exception instance.\n */\n setHelp(help: string): this {\n this.help = help\n return this\n }\n\n /**\n * Assigns a programmatic error code and returns the exception instance\n * to allow for method chaining.\n *\n * @param code - The string error code (e.g., 'E_FILE_NOT_FOUND').\n * @returns The current exception instance.\n */\n setCode(code: string): this {\n this.code = code\n return this\n }\n\n /**\n * Assigns an HTTP-style status code and returns the exception instance\n * to allow for method chaining.\n *\n * @param status - The numeric status code (e.g., 404, 500).\n * @returns The current exception instance.\n */\n setStatus(status: number): this {\n this.status = status\n return this\n }\n\n /**\n * Provides the string tag used by `Object.prototype.toString`.\n */\n get [Symbol.toStringTag]() {\n return this.constructor.name\n }\n\n /**\n * Computes a string representation of the exception, including the\n * programmatic code if one is defined.\n */\n override toString(): string {\n if (this.code) {\n return `${this.name} [${this.code}]: ${this.message}`\n }\n return `${this.name}: ${this.message}`\n }\n\n /**\n * Serializes the exception into a plain JavaScript object.\n * This ensures safe passing across boundaries and correct format in JSON responses.\n *\n * @returns The serialized exception object matching `ExceptionSchema`.\n */\n toJSON(): ExceptionSchema {\n const result: ExceptionSchema = {\n name: this.name,\n message: this.message,\n }\n if (this.status) {\n result.status = this.status\n }\n if (this.code) {\n result.code = this.code\n }\n if (this.help) {\n result.help = this.help\n }\n return result\n }\n}\n\n/**\n * Converts a standard Error object (or thrown primitive) into an Exception instance.\n * This function extracts relevant properties from the raw error\n * and sets them on the Exception instance, allowing for a more\n * consistent error handling experience across the application.\n * @param error The unknown error value to convert.\n * @param defaults Optional default properties to apply if they are missing on the Error object.\n * @returns An Exception instance with properties set from the Error object.\n */\nexport function fromError(error: unknown, defaults: Partial<ExceptionSchema> = {}): Exception {\n let message: string | undefined\n let typed: Record<string, string | number> = {}\n\n if (error instanceof Error) {\n message = error.message\n typed = error as unknown as Record<string, string | number>\n } else if (typeof error === 'string') {\n message = error\n } else if (error !== null && typeof error === 'object') {\n typed = error as Record<string, string | number>\n if (typeof typed.message === 'string') {\n message = typed.message\n }\n }\n\n const e = new Exception(message || defaults.message || 'An error occurred')\n\n if (typed.code !== undefined) {\n e.code = String(typed.code)\n } else if (defaults.code !== undefined) {\n e.code = defaults.code\n }\n\n if (typed.status !== undefined) {\n const status = Number(typed.status)\n e.status = isNaN(status) ? (defaults.status ?? 500) : status\n } else if (defaults.status !== undefined) {\n e.status = defaults.status\n }\n\n if (typed.help !== undefined) {\n e.setHelp(String(typed.help))\n } else if (defaults.help !== undefined) {\n e.setHelp(defaults.help)\n }\n\n if (typed.name !== undefined) {\n e.name = String(typed.name)\n } else if (defaults.name !== undefined) {\n e.name = defaults.name\n }\n\n return e\n}\n"]}