@platformatic/itc 3.38.1 → 3.39.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 (2) hide show
  1. package/lib/index.js +55 -1
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -92,11 +92,65 @@ export function generateUnhandledErrorResponse (error) {
92
92
  }
93
93
  }
94
94
 
95
+ function sanitizeError (error, transferList) {
96
+ let needsSanitization = false
97
+
98
+ for (const value of Object.values(error)) {
99
+ const valueType = typeof value
100
+ if (valueType === 'function' || valueType === 'symbol') {
101
+ needsSanitization = true
102
+ break
103
+ }
104
+
105
+ if (value && typeof value === 'object') {
106
+ const sanitized = sanitize(value, transferList)
107
+ if (sanitized !== value) {
108
+ needsSanitization = true
109
+ break
110
+ }
111
+ }
112
+ }
113
+
114
+ if (!needsSanitization) {
115
+ return error
116
+ }
117
+
118
+ // Build a plain object with all safe error properties.
119
+ // We intentionally do NOT create a new Error() because @fastify/error instances
120
+ // (used by avvio and ITC errors) go through structured clone as plain objects,
121
+ // which preserves all enumerable own properties. A standard new Error() would
122
+ // instead be cloned as an Error, silently dropping custom properties like code.
123
+ const sanitized = {}
124
+ if (error.message) sanitized.message = error.message
125
+ if (error.name) sanitized.name = error.name
126
+ if (error.stack) sanitized.stack = error.stack
127
+ if (error.code) sanitized.code = error.code
128
+ if (error.statusCode) sanitized.statusCode = error.statusCode
129
+
130
+ for (const [key, value] of Object.entries(error)) {
131
+ if (key === 'message' || key === 'stack' || key === 'name' || key === 'code' || key === 'statusCode') continue
132
+ const valueType = typeof value
133
+ if (valueType === 'function' || valueType === 'symbol') continue
134
+
135
+ if (value && typeof value === 'object') {
136
+ sanitized[key] = sanitize(value, transferList)
137
+ } else {
138
+ sanitized[key] = value
139
+ }
140
+ }
141
+
142
+ return sanitized
143
+ }
144
+
95
145
  export function sanitize (data, transferList) {
96
- if (!data || typeof data !== 'object' || transferList?.includes(data) || data instanceof Error) {
146
+ if (!data || typeof data !== 'object' || transferList?.includes(data)) {
97
147
  return data
98
148
  }
99
149
 
150
+ if (data instanceof Error) {
151
+ return sanitizeError(data, transferList)
152
+ }
153
+
100
154
  if (Buffer.isBuffer(data) || data instanceof Uint8Array) {
101
155
  // This will convert as Uint8Array
102
156
  return data
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/itc",
3
- "version": "3.38.1",
3
+ "version": "3.39.0",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "type": "module",