|
12 | 12 |
|
13 | 13 | const { Object, Math } = primordials; |
14 | 14 |
|
15 | | -const kCode = Symbol('code'); |
16 | | -const kInfo = Symbol('info'); |
17 | 15 | const messages = new Map(); |
18 | 16 | const codes = {}; |
19 | 17 |
|
@@ -121,76 +119,86 @@ class SystemError extends Error { |
121 | 119 | writable: true, |
122 | 120 | configurable: true |
123 | 121 | }); |
124 | | - Object.defineProperty(this, kInfo, { |
125 | | - configurable: false, |
126 | | - enumerable: false, |
127 | | - value: context |
128 | | - }); |
129 | | - Object.defineProperty(this, kCode, { |
130 | | - configurable: true, |
131 | | - enumerable: false, |
132 | | - value: key, |
133 | | - writable: true |
134 | | - }); |
135 | 122 | addCodeToName(this, 'SystemError', key); |
136 | | - } |
137 | 123 |
|
138 | | - get code() { |
139 | | - return this[kCode]; |
140 | | - } |
| 124 | + this.code = key; |
141 | 125 |
|
142 | | - set code(value) { |
143 | | - Object.defineProperty(this, 'code', { |
144 | | - configurable: true, |
| 126 | + Object.defineProperty(this, 'info', { |
| 127 | + value: context, |
145 | 128 | enumerable: true, |
146 | | - value, |
147 | | - writable: true |
| 129 | + configurable: true, |
| 130 | + writable: false |
148 | 131 | }); |
149 | | - } |
150 | | - |
151 | | - get info() { |
152 | | - return this[kInfo]; |
153 | | - } |
154 | 132 |
|
155 | | - get errno() { |
156 | | - return this[kInfo].errno; |
157 | | - } |
158 | | - |
159 | | - set errno(val) { |
160 | | - this[kInfo].errno = val; |
161 | | - } |
162 | | - |
163 | | - get syscall() { |
164 | | - return this[kInfo].syscall; |
165 | | - } |
166 | | - |
167 | | - set syscall(val) { |
168 | | - this[kInfo].syscall = val; |
169 | | - } |
170 | | - |
171 | | - get path() { |
172 | | - return this[kInfo].path !== undefined ? |
173 | | - this[kInfo].path.toString() : undefined; |
174 | | - } |
| 133 | + Object.defineProperty(this, 'errno', { |
| 134 | + get() { |
| 135 | + return context.errno; |
| 136 | + }, |
| 137 | + set: (value) => { |
| 138 | + context.errno = value; |
| 139 | + }, |
| 140 | + enumerable: true, |
| 141 | + configurable: true |
| 142 | + }); |
175 | 143 |
|
176 | | - set path(val) { |
177 | | - this[kInfo].path = val ? |
178 | | - lazyBuffer().from(val.toString()) : undefined; |
179 | | - } |
| 144 | + Object.defineProperty(this, 'syscall', { |
| 145 | + get() { |
| 146 | + return context.syscall; |
| 147 | + }, |
| 148 | + set: (value) => { |
| 149 | + context.syscall = value; |
| 150 | + }, |
| 151 | + enumerable: true, |
| 152 | + configurable: true |
| 153 | + }); |
180 | 154 |
|
181 | | - get dest() { |
182 | | - return this[kInfo].path !== undefined ? |
183 | | - this[kInfo].dest.toString() : undefined; |
184 | | - } |
| 155 | + if (context.path !== undefined) { |
| 156 | + // TODO(BridgeAR): Investigate why and when the `.toString()` was |
| 157 | + // introduced. The `path` and `dest` properties in the context seem to |
| 158 | + // always be of type string. We should probably just remove the |
| 159 | + // `.toString()` and `Buffer.from()` operations and set the value on the |
| 160 | + // context as the user did. |
| 161 | + Object.defineProperty(this, 'path', { |
| 162 | + get() { |
| 163 | + return context.path != null ? |
| 164 | + context.path.toString() : context.path; |
| 165 | + }, |
| 166 | + set: (value) => { |
| 167 | + context.path = value ? |
| 168 | + lazyBuffer().from(value.toString()) : undefined; |
| 169 | + }, |
| 170 | + enumerable: true, |
| 171 | + configurable: true |
| 172 | + }); |
| 173 | + } |
185 | 174 |
|
186 | | - set dest(val) { |
187 | | - this[kInfo].dest = val ? |
188 | | - lazyBuffer().from(val.toString()) : undefined; |
| 175 | + if (context.dest !== undefined) { |
| 176 | + Object.defineProperty(this, 'dest', { |
| 177 | + get() { |
| 178 | + return context.dest != null ? |
| 179 | + context.dest.toString() : context.dest; |
| 180 | + }, |
| 181 | + set: (value) => { |
| 182 | + context.dest = value ? |
| 183 | + lazyBuffer().from(value.toString()) : undefined; |
| 184 | + }, |
| 185 | + enumerable: true, |
| 186 | + configurable: true |
| 187 | + }); |
| 188 | + } |
189 | 189 | } |
190 | 190 |
|
191 | 191 | toString() { |
192 | 192 | return `${this.name} [${this.code}]: ${this.message}`; |
193 | 193 | } |
| 194 | + |
| 195 | + [Symbol.for('nodejs.util.inspect.custom')](recurseTimes, ctx) { |
| 196 | + return lazyInternalUtilInspect().inspect(this, { |
| 197 | + ...ctx, |
| 198 | + getters: true, |
| 199 | + customInspect: false |
| 200 | + }); |
| 201 | + } |
194 | 202 | } |
195 | 203 |
|
196 | 204 | function makeSystemErrorWithCode(key) { |
@@ -221,19 +229,7 @@ function makeNodeErrorWithCode(Base, key) { |
221 | 229 | configurable: true |
222 | 230 | }); |
223 | 231 | addCodeToName(this, super.name, key); |
224 | | - } |
225 | | - |
226 | | - get code() { |
227 | | - return key; |
228 | | - } |
229 | | - |
230 | | - set code(value) { |
231 | | - Object.defineProperty(this, 'code', { |
232 | | - configurable: true, |
233 | | - enumerable: true, |
234 | | - value, |
235 | | - writable: true |
236 | | - }); |
| 232 | + this.code = key; |
237 | 233 | } |
238 | 234 |
|
239 | 235 | toString() { |
@@ -394,7 +390,6 @@ function uvException(ctx) { |
394 | 390 | err[prop] = ctx[prop]; |
395 | 391 | } |
396 | 392 |
|
397 | | - // TODO(BridgeAR): Show the `code` property as part of the stack. |
398 | 393 | err.code = code; |
399 | 394 | if (path) { |
400 | 395 | err.path = path; |
|
0 commit comments