@zudojs/errors 1.0.1 → 1.2.0

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.
Files changed (76) hide show
  1. package/README.md +9 -2
  2. package/dist/base/core/baseError.core.d.ts +18 -4
  3. package/dist/base/core/baseError.core.js +31 -46
  4. package/dist/base/core/baseError.serialize.d.ts +53 -0
  5. package/dist/base/core/baseError.serialize.js +109 -0
  6. package/dist/base/core/errorCause.redact.d.ts +25 -0
  7. package/dist/base/core/errorCause.redact.js +75 -0
  8. package/dist/base/types/errorCode.type.d.ts +11 -1
  9. package/dist/base/types/errorCode.type.js +12 -0
  10. package/dist/domain/access/authError.base.d.ts +25 -0
  11. package/dist/domain/access/authError.base.js +26 -0
  12. package/dist/domain/access/index.d.ts +2 -0
  13. package/dist/domain/access/index.js +2 -0
  14. package/dist/domain/access/oauthError.base.d.ts +24 -0
  15. package/dist/domain/access/oauthError.base.js +30 -0
  16. package/dist/domain/cli/cliError.base.d.ts +24 -0
  17. package/dist/domain/cli/cliError.base.js +34 -0
  18. package/dist/domain/cli/index.d.ts +7 -0
  19. package/dist/domain/cli/index.js +7 -0
  20. package/dist/domain/constant/constantError.base.d.ts +21 -0
  21. package/dist/domain/constant/constantError.base.js +31 -0
  22. package/dist/domain/constant/index.d.ts +7 -0
  23. package/dist/domain/constant/index.js +7 -0
  24. package/dist/domain/cqrs/cqrsError.base.d.ts +14 -0
  25. package/dist/domain/cqrs/cqrsError.base.js +26 -0
  26. package/dist/domain/cqrs/index.d.ts +7 -0
  27. package/dist/domain/cqrs/index.js +7 -0
  28. package/dist/domain/event/event.error.d.ts +1 -1
  29. package/dist/domain/event/event.error.js +1 -1
  30. package/dist/domain/event/eventError.lifecycle.d.ts +16 -0
  31. package/dist/domain/event/eventError.lifecycle.js +31 -0
  32. package/dist/domain/index.d.ts +6 -0
  33. package/dist/domain/index.js +6 -0
  34. package/dist/domain/openapi/index.d.ts +7 -0
  35. package/dist/domain/openapi/index.js +7 -0
  36. package/dist/domain/openapi/openApiError.base.d.ts +20 -0
  37. package/dist/domain/openapi/openApiError.base.js +24 -0
  38. package/dist/domain/schema/schemaError.base.js +1 -1
  39. package/dist/domain/shared/domainError.helpers.d.ts +3 -2
  40. package/dist/domain/shared/domainError.helpers.js +32 -8
  41. package/dist/domain/state/validationError.base.d.ts +4 -3
  42. package/dist/domain/state/validationError.base.js +5 -4
  43. package/dist/domain/transaction/index.d.ts +10 -0
  44. package/dist/domain/transaction/index.js +10 -0
  45. package/dist/domain/transaction/transactionError.base.d.ts +18 -0
  46. package/dist/domain/transaction/transactionError.base.js +21 -0
  47. package/dist/domain/transaction/transactionError.misc.d.ts +26 -0
  48. package/dist/domain/transaction/transactionError.misc.js +47 -0
  49. package/dist/domain/transaction/transactionError.types.d.ts +33 -0
  50. package/dist/domain/transaction/transactionError.types.js +62 -0
  51. package/dist/domain/traversal/index.d.ts +7 -0
  52. package/dist/domain/traversal/index.js +7 -0
  53. package/dist/domain/traversal/traversalLimit.error.d.ts +37 -0
  54. package/dist/domain/traversal/traversalLimit.error.js +39 -0
  55. package/dist/infrastructure/httpPipeline/httpMiddleware.error.d.ts +26 -0
  56. package/dist/infrastructure/httpPipeline/httpMiddleware.error.js +43 -0
  57. package/dist/infrastructure/httpPipeline/httpRequestGuard.error.d.ts +21 -0
  58. package/dist/infrastructure/httpPipeline/httpRequestGuard.error.js +28 -0
  59. package/dist/infrastructure/httpPipeline/index.d.ts +8 -0
  60. package/dist/infrastructure/httpPipeline/index.js +8 -0
  61. package/dist/infrastructure/index.d.ts +1 -0
  62. package/dist/infrastructure/index.js +1 -0
  63. package/dist/infrastructure/middleware/index.d.ts +1 -0
  64. package/dist/infrastructure/middleware/index.js +1 -0
  65. package/dist/infrastructure/middleware/middlewareError.types.d.ts +28 -0
  66. package/dist/infrastructure/middleware/middlewareError.types.js +44 -0
  67. package/dist/system/index.d.ts +1 -0
  68. package/dist/system/index.js +1 -0
  69. package/dist/system/observability/index.d.ts +7 -0
  70. package/dist/system/observability/index.js +7 -0
  71. package/dist/system/observability/observabilityError.base.d.ts +16 -0
  72. package/dist/system/observability/observabilityError.base.js +23 -0
  73. package/dist/utils/errorMapper.mappers.d.ts +1 -1
  74. package/dist/utils/errorSerializer.core.d.ts +10 -2
  75. package/dist/utils/errorSerializer.core.js +30 -76
  76. package/package.json +1 -1
@@ -2,7 +2,9 @@
2
2
  * Converts errors into safe, predictable serialized structures.
3
3
  */
4
4
  import { BaseError } from "../base/core/baseError.core.js";
5
- import { isSensitiveMetadataKey, pickErrorMetadata, redactErrorMetadata, REDACTED_METADATA_VALUE, } from "../base/core/errorMetadata.core.js";
5
+ import { pickErrorMetadata, redactErrorMetadata, } from "../base/core/errorMetadata.core.js";
6
+ import { isGenuineSerializedBaseError, MAX_CAUSE_DEPTH, toJSONWithFrame, } from "../base/core/baseError.serialize.js";
7
+ import { redactCauseFields, redactCauseValue, } from "../base/core/errorCause.redact.js";
6
8
  import { normalizeUnknownError } from "./errorSerializer.factory.js";
7
9
  export { createErrorSerializer, serializeError, serializePublicError, normalizeUnknownError, } from "./errorSerializer.factory.js";
8
10
  /** Converts errors into safe, predictable serialized structures. */
@@ -27,10 +29,12 @@ export class ErrorSerializer {
27
29
  * Serializes an error for internal logging or monitoring.
28
30
  *
29
31
  * `includeStack`, `includeCause` and `redactSensitiveData` are applied at
30
- * every level of the cause chain.
32
+ * every level of the cause chain, including array causes and plain-object
33
+ * causes that merely look like a serialized BaseError.
31
34
  */
32
35
  serialize(error) {
33
- return this.serializeLevel(error.toJSON());
36
+ const raw = toJSONWithFrame(error, { depth: 0, redact: false });
37
+ return this.serializeLevel(raw, 0);
34
38
  }
35
39
  /**
36
40
  * Serializes an error for an untrusted API client.
@@ -58,7 +62,7 @@ export class ErrorSerializer {
58
62
  return this.serializePublic(normalizeUnknownError(value));
59
63
  }
60
64
  /** Applies the serializer options to one level of a serialized error. */
61
- serializeLevel(serialized) {
65
+ serializeLevel(serialized, depth) {
62
66
  const { stack, cause, metadata, ...rest } = serialized;
63
67
  const result = {
64
68
  ...rest,
@@ -69,25 +73,33 @@ export class ErrorSerializer {
69
73
  : {},
70
74
  ...(this.includeStack && stack !== undefined ? { stack } : {}),
71
75
  ...(this.includeCause && cause !== undefined
72
- ? { cause: this.serializeCause(cause) }
76
+ ? { cause: this.serializeCause(cause, depth + 1) }
73
77
  : {}),
74
78
  };
75
79
  return result;
76
80
  }
77
- /** Applies the serializer options recursively to a serialized cause. */
78
- serializeCause(cause) {
81
+ /**
82
+ * Applies the serializer options recursively to a serialized cause.
83
+ *
84
+ * Only objects that a BaseError's `toJSON` really produced are treated as
85
+ * serialized BaseErrors (whose `metadata` alone needs redacting). Anything
86
+ * else, including a plain object carrying the same field names and any
87
+ * array, has every field walked and redacted.
88
+ */
89
+ serializeCause(cause, depth) {
90
+ if (depth > MAX_CAUSE_DEPTH)
91
+ return "[MaxDepth]";
79
92
  if (cause === null || typeof cause !== "object")
80
93
  return cause;
81
- if (Array.isArray(cause))
82
- return cause;
94
+ if (Array.isArray(cause)) {
95
+ return this.redactSensitiveData
96
+ ? redactCauseValue(cause, this.sensitiveKeyPattern)
97
+ : cause;
98
+ }
83
99
  const record = cause;
84
- if (isSerializedBaseError(record)) {
85
- return this.serializeLevel(record);
100
+ if (isGenuineSerializedBaseError(record)) {
101
+ return this.serializeLevel(record, depth);
86
102
  }
87
- // Native error shape: { name, message, stack?, cause? } — or an
88
- // arbitrary plain object that was thrown/attached as a cause. The
89
- // latter reaches here by reference and used to be copied verbatim, so
90
- // `redactSensitiveData` did not apply to it.
91
103
  const { stack, cause: nested, ...rest } = record;
92
104
  const fields = this.redactSensitiveData
93
105
  ? redactCauseFields(rest, this.sensitiveKeyPattern, new WeakSet([record]))
@@ -95,7 +107,9 @@ export class ErrorSerializer {
95
107
  return {
96
108
  ...fields,
97
109
  ...(this.includeStack && stack !== undefined ? { stack } : {}),
98
- ...(nested !== undefined ? { cause: this.serializeCause(nested) } : {}),
110
+ ...(nested !== undefined
111
+ ? { cause: this.serializeCause(nested, depth + 1) }
112
+ : {}),
99
113
  };
100
114
  }
101
115
  /** Computes the metadata allowed in a public response. */
@@ -124,64 +138,4 @@ export class ErrorSerializer {
124
138
  });
125
139
  }
126
140
  }
127
- /** Plain objects (Object.prototype or null prototype) are walked; anything else is kept as-is. */
128
- function isPlainRecord(value) {
129
- if (value === null || typeof value !== "object")
130
- return false;
131
- const proto = Object.getPrototypeOf(value);
132
- return proto === Object.prototype || proto === null;
133
- }
134
- /**
135
- * Redacts sensitive keys inside a non-BaseError cause without changing its
136
- * shape: primitives, dates and class instances are kept, plain objects and
137
- * arrays are walked, cycles stop at "[Circular]".
138
- */
139
- function redactCauseFields(fields, pattern, seen = new WeakSet()) {
140
- const result = {};
141
- for (const key of Object.keys(fields)) {
142
- if (key === "__proto__" || key === "constructor" || key === "prototype")
143
- continue;
144
- const value = fields[key];
145
- const sensitive = pattern === undefined
146
- ? isSensitiveMetadataKey(key)
147
- : isSensitiveMetadataKey(key, pattern);
148
- result[key] = sensitive
149
- ? REDACTED_METADATA_VALUE
150
- : redactCauseValue(value, pattern, seen);
151
- }
152
- return result;
153
- }
154
- function redactCauseValue(value, pattern, seen) {
155
- if (Array.isArray(value)) {
156
- if (seen.has(value))
157
- return "[Circular]";
158
- seen.add(value);
159
- try {
160
- return value.map((entry) => redactCauseValue(entry, pattern, seen));
161
- }
162
- finally {
163
- seen.delete(value);
164
- }
165
- }
166
- if (!isPlainRecord(value))
167
- return value;
168
- if (seen.has(value))
169
- return "[Circular]";
170
- seen.add(value);
171
- try {
172
- return redactCauseFields(value, pattern, seen);
173
- }
174
- finally {
175
- seen.delete(value);
176
- }
177
- }
178
- /** Structural check for a serialized BaseError (used on nested causes). */
179
- function isSerializedBaseError(value) {
180
- return (typeof value.code === "string" &&
181
- typeof value.category === "string" &&
182
- typeof value.severity === "string" &&
183
- typeof value.statusCode === "number" &&
184
- typeof value.metadata === "object" &&
185
- value.metadata !== null);
186
- }
187
141
  //# sourceMappingURL=errorSerializer.core.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zudojs/errors",
3
- "version": "1.0.1",
3
+ "version": "1.2.0",
4
4
  "description": "Shared error base class, error codes, and error handling utilities for the Zudojs framework.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",