Skip to content

Commit a0c117b

Browse files
vsemozhetbytaddaleax
authored andcommitted
doc: fixup errors.md
* add semicolons in examples * fix indentation in code example * add spaces in code examples * console.log() -> console.error() * fix level of headings * update comment code example * delete obsolete info and example Fixes: #11558 PR-URL: #11566 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 2601c06 commit a0c117b

File tree

1 file changed

+16
-29
lines changed

1 file changed

+16
-29
lines changed

doc/api/errors.md

+16-29
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,15 @@ the first argument will be passed as `null`.
141141
const fs = require('fs');
142142

143143
function nodeStyleCallback(err, data) {
144-
if (err) {
145-
console.error('There was an error', err);
146-
return;
147-
}
148-
console.log(data);
144+
if (err) {
145+
console.error('There was an error', err);
146+
return;
147+
}
148+
console.log(data);
149149
}
150150

151151
fs.readFile('/some/file/that/does-not-exist', nodeStyleCallback);
152-
fs.readFile('/some/file/that/does-exist', nodeStyleCallback)
152+
fs.readFile('/some/file/that/does-exist', nodeStyleCallback);
153153
```
154154

155155
The JavaScript `try / catch` mechanism **cannot** be used to intercept errors
@@ -167,15 +167,15 @@ try {
167167
throw err;
168168
}
169169
});
170-
} catch(err) {
170+
} catch (err) {
171171
// This will not catch the throw!
172-
console.log(err);
172+
console.error(err);
173173
}
174174
```
175175

176176
This will not work because the callback function passed to `fs.readFile()` is
177177
called asynchronously. By the time the callback has been called, the
178-
surrounding code (including the `try { } catch(err) { }` block will have
178+
surrounding code (including the `try { } catch (err) { }` block will have
179179
already exited. Throwing an error inside the callback **can crash the Node.js
180180
process** in most cases. If [domains][] are enabled, or a handler has been
181181
registered with `process.on('uncaughtException')`, such errors can be
@@ -217,7 +217,7 @@ a string representing the location in the code at which
217217
```js
218218
const myObject = {};
219219
Error.captureStackTrace(myObject);
220-
myObject.stack // similar to `new Error().stack`
220+
myObject.stack; // similar to `new Error().stack`
221221
```
222222

223223
The first line of the trace, instead of being prefixed with `ErrorType:
@@ -238,7 +238,7 @@ function MyError() {
238238
// Without passing MyError to captureStackTrace, the MyError
239239
// frame would show up in the .stack property. By passing
240240
// the constructor, we omit that frame and all frames above it.
241-
new MyError().stack
241+
new MyError().stack;
242242
```
243243

244244
### Error.stackTraceLimit
@@ -255,7 +255,7 @@ will affect any stack trace captured *after* the value has been changed.
255255
If set to a non-number value, or set to a negative number, stack traces will
256256
not capture any frames.
257257

258-
#### error.message
258+
### error.message
259259

260260
* {String}
261261

@@ -267,11 +267,11 @@ the stack trace of the `Error`, however changing this property after the
267267

268268
```js
269269
const err = new Error('The message');
270-
console.log(err.message);
270+
console.error(err.message);
271271
// Prints: The message
272272
```
273273

274-
#### error.stack
274+
### error.stack
275275

276276
* {String}
277277

@@ -359,7 +359,7 @@ For example:
359359

360360
```js
361361
require('net').connect(-1);
362-
// throws RangeError, port should be > 0 && < 65536
362+
// throws "RangeError: "port" option should be >= 0 and < 65536: -1"
363363
```
364364

365365
Node.js will generate and throw `RangeError` instances *immediately* as a form
@@ -379,19 +379,6 @@ doesNotExist;
379379
// throws ReferenceError, doesNotExist is not a variable in this program.
380380
```
381381

382-
`ReferenceError` instances will have an `error.arguments` property whose value
383-
is an array containing a single element: a string representing the variable
384-
that was not defined.
385-
386-
```js
387-
const assert = require('assert');
388-
try {
389-
doesNotExist;
390-
} catch(err) {
391-
assert(err.arguments[0], 'doesNotExist');
392-
}
393-
```
394-
395382
Unless an application is dynamically generating and running code,
396383
`ReferenceError` instances should always be considered a bug in the code
397384
or its dependencies.
@@ -407,7 +394,7 @@ program.
407394
```js
408395
try {
409396
require('vm').runInThisContext('binary ! isNotOk');
410-
} catch(err) {
397+
} catch (err) {
411398
// err will be a SyntaxError
412399
}
413400
```

0 commit comments

Comments
 (0)