Browse Source

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: https://github.com/nodejs/node/issues/11558
PR-URL: https://github.com/nodejs/node/pull/11566
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
v6
Vse Mozhet Byt 8 years ago
committed by James M Snell
parent
commit
6aa18aae39
  1. 35
      doc/api/errors.md

35
doc/api/errors.md

@ -149,7 +149,7 @@ function nodeStyleCallback(err, data) {
} }
fs.readFile('/some/file/that/does-not-exist', nodeStyleCallback); fs.readFile('/some/file/that/does-not-exist', nodeStyleCallback);
fs.readFile('/some/file/that/does-exist', nodeStyleCallback) fs.readFile('/some/file/that/does-exist', nodeStyleCallback);
``` ```
The JavaScript `try / catch` mechanism **cannot** be used to intercept errors The JavaScript `try / catch` mechanism **cannot** be used to intercept errors
@ -167,15 +167,15 @@ try {
throw err; throw err;
} }
}); });
} catch(err) { } catch (err) {
// This will not catch the throw! // This will not catch the throw!
console.log(err); console.error(err);
} }
``` ```
This will not work because the callback function passed to `fs.readFile()` is This will not work because the callback function passed to `fs.readFile()` is
called asynchronously. By the time the callback has been called, the called asynchronously. By the time the callback has been called, the
surrounding code (including the `try { } catch(err) { }` block will have surrounding code (including the `try { } catch (err) { }` block will have
already exited. Throwing an error inside the callback **can crash the Node.js already exited. Throwing an error inside the callback **can crash the Node.js
process** in most cases. If [domains][] are enabled, or a handler has been process** in most cases. If [domains][] are enabled, or a handler has been
registered with `process.on('uncaughtException')`, such errors can be registered with `process.on('uncaughtException')`, such errors can be
@ -217,7 +217,7 @@ a string representing the location in the code at which
```js ```js
const myObject = {}; const myObject = {};
Error.captureStackTrace(myObject); Error.captureStackTrace(myObject);
myObject.stack // similar to `new Error().stack` myObject.stack; // similar to `new Error().stack`
``` ```
The first line of the trace, instead of being prefixed with `ErrorType: The first line of the trace, instead of being prefixed with `ErrorType:
@ -238,7 +238,7 @@ function MyError() {
// Without passing MyError to captureStackTrace, the MyError // Without passing MyError to captureStackTrace, the MyError
// frame would show up in the .stack property. By passing // frame would show up in the .stack property. By passing
// the constructor, we omit that frame and all frames above it. // the constructor, we omit that frame and all frames above it.
new MyError().stack new MyError().stack;
``` ```
### Error.stackTraceLimit ### Error.stackTraceLimit
@ -255,7 +255,7 @@ will affect any stack trace captured *after* the value has been changed.
If set to a non-number value, or set to a negative number, stack traces will If set to a non-number value, or set to a negative number, stack traces will
not capture any frames. not capture any frames.
#### error.message ### error.message
* {String} * {String}
@ -267,11 +267,11 @@ the stack trace of the `Error`, however changing this property after the
```js ```js
const err = new Error('The message'); const err = new Error('The message');
console.log(err.message); console.error(err.message);
// Prints: The message // Prints: The message
``` ```
#### error.stack ### error.stack
* {String} * {String}
@ -359,7 +359,7 @@ For example:
```js ```js
require('net').connect(-1); require('net').connect(-1);
// throws RangeError, port should be > 0 && < 65536 // throws "RangeError: "port" option should be >= 0 and < 65536: -1"
``` ```
Node.js will generate and throw `RangeError` instances *immediately* as a form Node.js will generate and throw `RangeError` instances *immediately* as a form
@ -379,19 +379,6 @@ doesNotExist;
// throws ReferenceError, doesNotExist is not a variable in this program. // throws ReferenceError, doesNotExist is not a variable in this program.
``` ```
`ReferenceError` instances will have an `error.arguments` property whose value
is an array containing a single element: a string representing the variable
that was not defined.
```js
const assert = require('assert');
try {
doesNotExist;
} catch(err) {
assert(err.arguments[0], 'doesNotExist');
}
```
Unless an application is dynamically generating and running code, Unless an application is dynamically generating and running code,
`ReferenceError` instances should always be considered a bug in the code `ReferenceError` instances should always be considered a bug in the code
or its dependencies. or its dependencies.
@ -407,7 +394,7 @@ program.
```js ```js
try { try {
require('vm').runInThisContext('binary ! isNotOk'); require('vm').runInThisContext('binary ! isNotOk');
} catch(err) { } catch (err) {
// err will be a SyntaxError // err will be a SyntaxError
} }
``` ```

Loading…
Cancel
Save