-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR-URL: #789 Reviewed-by: Rod Vagg <[email protected]> Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]>
- v23.4.0
- v23.3.0
- v23.2.0
- v23.1.0
- v23.0.0
- v22.12.0
- v22.11.0
- v22.10.0
- v22.9.0
- v22.8.0
- v22.7.0
- v22.6.0
- v22.5.1
- v22.5.0
- v22.4.1
- v22.4.0
- v22.3.0
- v22.2.0
- v22.1.0
- v22.0.0
- v21.7.3
- v21.7.2
- v21.7.1
- v21.7.0
- v21.6.2
- v21.6.1
- v21.6.0
- v21.5.0
- v21.4.0
- v21.3.0
- v21.2.0
- v21.1.0
- v21.0.0
- v20.18.1
- v20.18.0
- v20.17.0
- v20.16.0
- v20.15.1
- v20.15.0
- v20.14.0
- v20.13.1
- v20.13.0
- v20.12.2
- v20.12.1
- v20.12.0
- v20.11.1
- v20.11.0
- v20.10.0
- v20.9.0
- v20.8.1
- v20.8.0
- v20.7.0
- v20.6.1
- v20.6.0
- v20.5.1
- v20.5.0
- v20.4.0
- v20.3.1
- v20.3.0
- v20.2.0
- v20.1.0
- v20.0.0
- v19.9.0
- v19.8.1
- v19.8.0
- v19.7.0
- v19.6.1
- v19.6.0
- v19.5.0
- v19.4.0
- v19.3.0
- v19.2.0
- v19.1.0
- v19.0.1
- v19.0.0
- v18.20.5
- v18.20.4
- v18.20.3
- v18.20.2
- v18.20.1
- v18.20.0
- v18.19.1
- v18.19.0
- v18.18.2
- v18.18.1
- v18.18.0
- v18.17.1
- v18.17.0
- v18.16.1
- v18.16.0
- v18.15.0
- v18.14.2
- v18.14.1
- v18.14.0
- v18.13.0
- v18.12.1
- v18.12.0
- v18.11.0
- v18.10.0
- v18.9.1
- v18.9.0
- v18.8.0
- v18.7.0
- v18.6.0
- v18.5.0
- v18.4.0
- v18.3.0
- v18.2.0
- v18.1.0
- v18.0.0
- v17.9.1
- v17.9.0
- v17.8.0
- v17.7.2
- v17.7.1
- v17.7.0
- v17.6.0
- v17.5.0
- v17.4.0
- v17.3.1
- v17.3.0
- v17.2.0
- v17.1.0
- v17.0.1
- v17.0.0
- v16.20.2
- v16.20.1
- v16.20.0
- v16.19.1
- v16.19.0
- v16.18.1
- v16.18.0
- v16.17.1
- v16.17.0
- v16.16.0
- v16.15.1
- v16.15.0
- v16.14.2
- v16.14.1
- v16.14.0
- v16.13.2
- v16.13.1
- v16.13.0
- v16.12.0
- v16.11.1
- v16.11.0
- v16.10.0
- v16.9.1
- v16.9.0
- v16.8.0
- v16.7.0
- v16.6.2
- v16.6.1
- v16.6.0
- v16.5.0
- v16.4.2
- v16.4.1
- v16.4.0
- v16.3.0
- v16.2.0
- v16.1.0
- v16.0.0
- v15.14.0
- v15.13.0
- v15.12.0
- v15.11.0
- v15.10.0
- v15.9.0
- v15.8.0
- v15.7.0
- v15.6.0
- v15.5.1
- v15.5.0
- v15.4.0
- v15.3.0
- v15.2.1
- v15.2.0
- v15.1.0
- v15.0.1
- v15.0.0
- v14.21.3
- v14.21.2
- v14.21.1
- v14.21.0
- v14.20.1
- v14.20.0
- v14.19.3
- v14.19.2
- v14.19.1
- v14.19.0
- v14.18.3
- v14.18.2
- v14.18.1
- v14.18.0
- v14.17.6
- v14.17.5
- v14.17.4
- v14.17.3
- v14.17.2
- v14.17.1
- v14.17.0
- v14.16.1
- v14.16.0
- v14.15.5
- v14.15.4
- v14.15.3
- v14.15.2
- v14.15.1
- v14.15.0
- v14.14.0
- v14.13.1
- v14.13.0
- v14.12.0
- v14.11.0
- v14.10.1
- v14.10.0
- v14.9.0
- v14.8.0
- v14.7.0
- v14.6.0
- v14.5.0
- v14.4.0
- v14.3.0
- v14.2.0
- v14.1.0
- v14.0.0
- v13.14.0
- v13.13.0
- v13.12.0
- v13.11.0
- v13.10.1
- v13.10.0
- v13.9.0
- v13.8.0
- v13.7.0
- v13.6.0
- v13.5.0
- v13.4.0
- v13.3.0
- v13.2.0
- v13.1.0
- v13.0.1
- v13.0.0
- v12.22.12
- v12.22.11
- v12.22.10
- v12.22.9
- v12.22.8
- v12.22.7
- v12.22.6
- v12.22.5
- v12.22.4
- v12.22.3
- v12.22.2
- v12.22.1
- v12.22.0
- v12.21.0
- v12.20.2
- v12.20.1
- v12.20.0
- v12.19.1
- v12.19.0
- v12.18.4
- v12.18.3
- v12.18.2
- v12.18.1
- v12.18.0
- v12.17.0
- v12.16.3
- v12.16.2
- v12.16.1
- v12.16.0
- v12.15.0
- v12.14.1
- v12.14.0
- v12.13.1
- v12.13.0
- v12.12.0
- v12.11.1
- v12.11.0
- v12.10.0
- v12.9.1
- v12.9.0
- v12.8.1
- v12.8.0
- v12.7.0
- v12.6.0
- v12.5.0
- v12.4.0
- v12.3.1
- v12.3.0
- v12.2.0
- v12.1.0
- v12.0.0
- v11.15.0
- v11.14.0
- v11.13.0
- v11.12.0
- v11.11.0
- v11.10.1
- v11.10.0
- v11.9.0
- v11.8.0
- v11.7.0
- v11.6.0
- v11.5.0
- v11.4.0
- v11.3.0
- v11.2.0
- v11.1.0
- v11.0.0
- v10.24.1
- v10.24.0
- v10.23.3
- v10.23.2
- v10.23.1
- v10.23.0
- v10.22.1
- v10.22.0
- v10.21.0
- v10.20.1
- v10.20.0
- v10.19.0
- v10.18.1
- v10.18.0
- v10.17.0
- v10.16.3
- v10.16.2
- v10.16.1
- v10.16.0
- v10.15.3
- v10.15.2
- v10.15.1
- v10.15.0
- v10.14.2
- v10.14.1
- v10.14.0
- v10.13.0
- v10.12.0
- v10.11.0
- v10.10.0
- v10.9.0
- v10.8.0
- v10.7.0
- v10.6.0
- v10.5.0
- v10.4.1
- v10.4.0
- v10.3.0
- v10.2.1
- v10.2.0
- v10.1.0
- v10.0.0
- v9.11.2
- v9.11.1
- v9.11.0
- v9.10.1
- v9.10.0
- v9.9.0
- v9.8.0
- v9.7.1
- v9.7.0
- v9.6.1
- v9.6.0
- v9.5.0
- v9.4.0
- v9.3.0
- v9.2.1
- v9.2.0
- v9.1.0
- v9.0.0
- v8.17.0
- v8.16.2
- v8.16.1
- v8.16.0
- v8.15.1
- v8.15.0
- v8.14.1
- v8.14.0
- v8.13.0
- v8.12.0
- v8.11.4
- v8.11.3
- v8.11.2
- v8.11.1
- v8.11.0
- v8.10.0
- v8.9.4
- v8.9.3
- v8.9.2
- v8.9.1
- v8.9.0
- v8.8.1
- v8.8.0
- v8.7.0
- v8.6.0
- v8.5.0
- v8.4.0
- v8.3.0
- v8.2.1
- v8.2.0
- v8.1.4
- v8.1.3
- v8.1.2
- v8.1.1
- v8.1.0
- v8.0.0
- v7.10.1
- v7.10.0
- v7.9.0
- v7.8.0
- v7.7.4
- v7.7.3
- v7.7.2
- v7.7.1
- v7.7.0
- v7.6.0
- v7.5.0
- v7.4.0
- v7.3.0
- v7.2.1
- v7.2.0
- v7.1.0
- v7.0.0
- v6.17.1
- v6.17.0
- v6.16.0
- v6.15.1
- v6.15.0
- v6.14.4
- v6.14.3
- v6.14.2
- v6.14.1
- v6.14.0
- v6.13.1
- v6.13.0
- v6.12.3
- v6.12.2
- v6.12.1
- v6.12.0
- v6.11.5
- v6.11.4
- v6.11.3
- v6.11.2
- v6.11.1
- v6.11.0
- v6.10.3
- v6.10.2
- v6.10.1
- v6.10.0
- v6.9.5
- v6.9.4
- v6.9.3
- v6.9.2
- v6.9.1
- v6.9.0
- v6.8.1
- v6.8.0
- v6.7.0
- v6.6.0
- v6.5.0
- v6.4.0
- v6.3.1
- v6.3.0
- v6.2.2
- v6.2.1
- v6.2.0
- v6.1.0
- v6.0.0
- v5.12.0
- v5.11.1
- v5.11.0
- v5.10.1
- v5.10.0
- v5.9.1
- v5.9.0
- v5.8.1-rc.1
- v5.8.0
- v5.7.1
- v5.7.0
- v5.6.0
- v5.5.0
- v5.4.1
- v5.4.0
- v5.3.0
- v5.2.0
- v5.1.1
- v5.1.0
- v5.0.0
- v5.0.0-rc.2
- v5.0.0-rc.1
- v4.9.1
- v4.9.0
- v4.8.7
- v4.8.6
- v4.8.5
- v4.8.4
- v4.8.3
- v4.8.2
- v4.8.1
- v4.8.0
- v4.7.3
- v4.7.2
- v4.7.1
- v4.7.0
- v4.6.2
- v4.6.1
- v4.6.0
- v4.5.0
- v4.4.7
- v4.4.6
- v4.4.5
- v4.4.4
- v4.4.3
- v4.4.2
- v4.4.1
- v4.4.0
- v4.4.0-rc.4
- v4.4.0-rc.3
- v4.4.0-rc.2
- v4.4.0-rc.1
- v4.3.2
- v4.3.1
- v4.3.1-rc.2
- v4.3.1-rc.1
- v4.3.0
- v4.2.6
- v4.2.5
- v4.2.4
- v4.2.4-rc.1
- v4.2.3
- v4.2.2
- v4.2.2-rc.2
- v4.2.2-rc.1
- v4.2.1
- v4.2.0
- v4.1.2
- v4.1.1
- v4.1.0
- v4.0.0
- v4.0.0-rc.5
- v4.0.0-rc.4
- v4.0.0-rc.3
- v4.0.0-rc.2
- v4.0.0-rc.1
- v3.3.1
- v3.3.0
- v3.2.0
- v3.1.0
- v3.0.0
- v3.0.0-rc.7
- v3.0.0-rc.6
- v3.0.0-rc.5
- v3.0.0-rc.4
- v3.0.0-rc.3
- v3.0.0-rc2
- v3.0.0-rc1
- v2.5.0
- v2.4.0
- v2.3.4
- v2.3.3
- v2.3.2
- v2.3.1
- v2.3.0
- v2.2.1
- v2.2.0
- v2.1.0
- v2.0.2
- v2.0.1
- v2.0.0
- v1.8.4
- v1.8.3
- v1.8.2
- v1.8.1
- v1.7.1
- v1.7.0
- v1.6.4
- v1.6.3
- v1.6.2
- v1.6.1
- v1.6.0
- v1.5.1
- v1.5.0
- v1.4.3
- v1.4.2
- v1.4.1
- v1.3.0
- v1.2.0
1 parent
d832be4
commit 7e2235a
Showing
3 changed files
with
472 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,470 @@ | ||
# Errors | ||
|
||
<!--type=misc--> | ||
|
||
Errors generated by io.js fall into two categories: JavaScript errors and system | ||
errors. All errors inherit from or are instances of JavaScript's [Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) | ||
class and are guaranteed to provide *at least* the attributes available on that | ||
class. | ||
|
||
When an operation is not permitted due to language-syntax or | ||
language-runtime-level reasons, a **JavaScript error** is generated and thrown | ||
as an **exception**. If an operation is not allowed due to system-level | ||
restrictions, a **system error** is generated. Client code is then given the | ||
opportunity to **intercept** this error based on how the API **propagates** it. | ||
|
||
The style of API called determines how generated errors are handed back, or | ||
**propagated**, to client code, which in turn informs how the client may **intercept** | ||
the error. Exceptions can be intercepted using the `try / catch` construct; | ||
other propagation strategies are covered [below](#errors_error_propagation_and_interception). | ||
|
||
## JavaScript Errors | ||
|
||
<!--type=misc--> | ||
|
||
JavaScript errors typically denote that an API is being used incorrectly, or that | ||
there is a problem with the program as written. | ||
|
||
### Class: Error | ||
|
||
<!--type=class--> | ||
|
||
A general error object. Unlike other error objects, `Error` instances do not | ||
denote any specific circumstance of why the error occurred. Errors capture a | ||
"stack trace" detailing the point in the program at which they were | ||
instantiated, and may provide a description of the error. | ||
|
||
**Note**: io.js will generate this class of error to encapsulate system | ||
errors as well as plain JavaScript errors. | ||
|
||
#### new Error(message) | ||
|
||
Instantiates a new Error object and sets its `.message` property to the provided | ||
message. Its `.stack` will represent the point in the program at which `new Error` | ||
was called. Stack traces are subject to [V8's stack trace API](https://code.google.com/p/v8-wiki/wiki/JavaScriptStackTraceApi). | ||
Stack traces only extend to the beginning of synchronous code execution, *or* a number of frames given by | ||
`Error.stackTraceLimit`, whichever is smaller. | ||
|
||
#### error.message | ||
|
||
A string of the value passed to `Error()` upon instantiation. The message will | ||
also appear in the first line of the stack trace of the error. Changing this | ||
property *may not* change the first line of the stack trace. | ||
|
||
#### error.stack | ||
|
||
A property that, when **accessed**, returns a string representing the point in the program | ||
at which this error was instantiated. An example stacktrace follows: | ||
|
||
Error: Things keep happening! | ||
at /home/gbusey/file.js:525:2 | ||
at Frobnicator.refrobulate (/home/gbusey/business-logic.js:424:21) | ||
at Actor.<anonymous> (/home/gbusey/actors.js:400:8) | ||
at increaseSynergy (/home/gbusey/actors.js:701:6) | ||
|
||
The first line is formatted as `<error class name>: <error message>`, and it is followed | ||
by a series of stack frames (each line beginning with "at "). Each frame describes | ||
a call site in the program that lead to the error being generated. V8 attempts to | ||
display a name for each function (by variable name, function name, or object | ||
method name), but occasionally it will not be able to find a suitable name. If | ||
V8 cannot determine a name for the function, only location information will be | ||
displayed for that frame. Otherwise, the determined function name will be displayed | ||
with location information appended in parentheses. | ||
|
||
Frames are **only** generated for JavaScript functions. If, for example, execution | ||
synchronously passes through a C++ addon function called `cheetahify`, which itself | ||
calls a JavaScript function, the frame representing the `cheetahify` call will **not** | ||
be present in stacktraces: | ||
|
||
```javascript | ||
var cheetahify = require('./native-binding.node'); | ||
|
||
function makeFaster() { | ||
// cheetahify *synchronously* calls speedy. | ||
cheetahify(function speedy() { | ||
throw new Error('oh no!'); | ||
}); | ||
} | ||
|
||
makeFaster(); // will throw: | ||
// /home/gbusey/file.js:6 | ||
// throw new Error('oh no!'); | ||
// ^ | ||
// Error: oh no! | ||
// at speedy (/home/gbusey/file.js:6:11) | ||
// at makeFaster (/home/gbusey/file.js:5:3) | ||
// at Object.<anonymous> (/home/gbusey/file.js:10:1) | ||
// at Module._compile (module.js:456:26) | ||
// at Object.Module._extensions..js (module.js:474:10) | ||
// at Module.load (module.js:356:32) | ||
// at Function.Module._load (module.js:312:12) | ||
// at Function.Module.runMain (module.js:497:10) | ||
// at startup (node.js:119:16) | ||
// at node.js:906:3 | ||
``` | ||
|
||
The location information will be one of: | ||
|
||
* `native`, if the frame represents a call internal to V8 (as in `[].forEach`). | ||
* `plain-filename.js:line:column`, if the frame represents a call internal to io.js. | ||
* `/absolute/path/to/file.js:line:column`, if the frame represents a call in a user program, or its dependencies. | ||
|
||
It is important to note that the string representing the stacktrace is only | ||
generated on **access**: it is lazily generated. | ||
|
||
The number of frames captured by the stack trace is bounded by the smaller of | ||
`Error.stackTraceLimit` or the number of available frames on the current event | ||
loop tick. | ||
|
||
System-level errors are generated as augmented Error instances, which are detailed | ||
[below](#errors_system_errors). | ||
|
||
#### Error.captureStackTrace(targetObject[, constructorOpt]) | ||
|
||
Creates a `.stack` property on `targetObject`, which when accessed returns | ||
a string representing the location in the program at which `Error.captureStackTrace` | ||
was called. | ||
|
||
```javascript | ||
var myObject = {}; | ||
|
||
Error.captureStackTrace(myObject); | ||
|
||
myObject.stack // similar to `new Error().stack` | ||
``` | ||
|
||
The first line of the trace, instead of being prefixed with `ErrorType: | ||
message`, will be the result of `targetObject.toString()`. | ||
|
||
`constructorOpt` optionally accepts a function. If given, all frames above | ||
`constructorOpt`, including `constructorOpt`, will be omitted from the generated | ||
stack trace. | ||
|
||
This is useful for hiding implementation details of error generation from the | ||
end user. A common way of using this parameter is to pass the current Error | ||
constructor to it: | ||
|
||
```javascript | ||
|
||
function MyError() { | ||
Error.captureStackTrace(this, MyError); | ||
} | ||
|
||
// without passing MyError to captureStackTrace, the MyError | ||
// frame would should up in the .stack property. by passing | ||
// the constructor, we omit that frame and all frames above it. | ||
new MyError().stack | ||
|
||
``` | ||
|
||
#### Error.stackTraceLimit | ||
|
||
Property that determines the number of stack frames collected by a stack trace | ||
(whether generated by `new Error().stack` or `Error.captureStackTrace(obj)`). | ||
|
||
The initial value is `10`. It may be set to any valid JavaScript number, which | ||
will effect any stack trace captured *after* the value has been changed. If set | ||
to a non-number value, stack traces will not capture any frames and will report | ||
`undefined` on access. | ||
|
||
### Class: RangeError | ||
|
||
A subclass of Error that indicates that a provided argument was not within the | ||
set or range of acceptable values for a function; whether that be a numeric | ||
range, or outside the set of options for a given function parameter. An example: | ||
|
||
```javascript | ||
require('net').connect(-1); // throws RangeError, port should be > 0 && < 65536 | ||
``` | ||
|
||
io.js will generate and throw RangeError instances *immediately* -- they are a form | ||
of argument validation. | ||
|
||
### Class: TypeError | ||
|
||
A subclass of Error that indicates that a provided argument is not an allowable | ||
type. For example, passing a function to a parameter which expects a string would | ||
be considered a TypeError. | ||
|
||
```javascript | ||
require('url').parse(function() { }); // throws TypeError, since it expected a string | ||
``` | ||
|
||
io.js will generate and throw TypeError instances *immediately* -- they are a form | ||
of argument validation. | ||
|
||
### Class: ReferenceError | ||
|
||
A subclass of Error that indicates that an attempt is being made to access a variable | ||
that is not defined. Most commonly it indicates a typo, or an otherwise broken program. | ||
While client code may generate and propagate these errors, in practice only V8 will do | ||
so. | ||
|
||
```javascript | ||
doesNotExist; // throws ReferenceError, doesNotExist is not a variable in this program. | ||
``` | ||
|
||
ReferenceError instances will have an `.arguments` member that is an array containing | ||
one element -- a string representing the variable that was not defined. | ||
|
||
```javascript | ||
try { | ||
doesNotExist; | ||
} catch(err) { | ||
err.arguments[0] === 'doesNotExist'; | ||
} | ||
``` | ||
|
||
Unless the userland program is dynamically generating and running code, | ||
ReferenceErrors should always be considered a bug in the program, or its | ||
dependencies. | ||
|
||
### Class: SyntaxError | ||
|
||
A subclass of Error that indicates that a program is not valid JavaScript. | ||
These errors may only be generated and propagated as a result of code | ||
evaluation. Code evaluation may happen as a result of `eval`, `Function`, | ||
`require`, or [vm](vm.html). These errors are almost always indicative of a broken | ||
program. | ||
|
||
```javascript | ||
try { | ||
require("vm").runInThisContext("binary ! isNotOk"); | ||
} catch(err) { | ||
// err will be a SyntaxError | ||
} | ||
``` | ||
|
||
SyntaxErrors are unrecoverable from the context that created them – they may only be caught | ||
by other contexts. | ||
|
||
### Exceptions vs. Errors | ||
|
||
<!--type=misc--> | ||
|
||
A JavaScript "exception" is a value that is thrown as a result of an invalid operation or | ||
as the target of a `throw` statement. While it is not required that these values inherit from | ||
`Error`, all exceptions thrown by io.js or the JavaScript runtime *will* be instances of Error. | ||
|
||
Some exceptions are *unrecoverable* at the JavaScript layer. These exceptions will always bring | ||
down the process. These are usually failed `assert()` checks or `abort()` calls in the C++ layer. | ||
|
||
## System Errors | ||
|
||
System errors are generated in response to a program's runtime environment. | ||
Ideally, they represent operational errors that the program needs to be able to | ||
react to. They are generated at the syscall level: an exhaustive list of error | ||
codes and their meanings is available by running `man 2 intro` on most Unices; | ||
or [online](http://man7.org/linux/man-pages/man2/intro.2.html). | ||
|
||
In io.js, system errors are represented as augmented Error objects -- not full | ||
subclasses, but instead an error instance with added members. | ||
|
||
### Class: System Error | ||
|
||
#### error.syscall | ||
|
||
A string representing the [syscall](http://man7.org/linux/man-pages/man2/syscall.2.html) that failed. | ||
|
||
#### error.errno | ||
#### error.code | ||
|
||
A string representing the error code, which is always `E` followed by capital | ||
letters, and may be referenced in `man 2 intro`. | ||
|
||
### Common System Errors | ||
|
||
This list is **not exhaustive**, but enumerates many of the common system errors when | ||
writing a io.js program. An exhaustive list may be found [here](http://man7.org/linux/man-pages/man2/intro.2.html). | ||
|
||
#### EPERM: Operation not permitted | ||
|
||
An attempt was made to perform an operation that requires appropriate | ||
privileges. | ||
|
||
#### ENOENT: No such file or directory | ||
|
||
Commonly raised by [fs](fs.html) operations; a component of the specified pathname | ||
does not exist -- no entity (file or directory) could be found by the given path. | ||
|
||
#### EACCES: Permission denied | ||
|
||
An attempt was made to access a file in a way forbidden by its file access | ||
permissions. | ||
|
||
#### EEXIST: File exists | ||
|
||
An existing file was the target of an operation that required that the target | ||
not exist. | ||
|
||
#### ENOTDIR: Not a directory | ||
|
||
A component of the given pathname existed, but was not a directory as expected. | ||
Commonly raised by [fs.readdir](fs.html#fs_fs_readdir_path_callback). | ||
|
||
#### EISDIR: Is a directory | ||
|
||
An operation expected a file, but the given pathname was a directory. | ||
|
||
#### EMFILE: Too many open files in system | ||
|
||
Maxiumum number of [file descriptors](http://en.wikipedia.org/wiki/File_descriptor) allowable on the system has | ||
been reached, and requests for another descriptor cannot be fulfilled until | ||
at least one has been closed. | ||
|
||
Commonly encountered when opening many files at once in parallel, especially | ||
on systems (in particular, OS X) where there is a low file descriptor limit | ||
for processes. To remedy a low limit, run `ulimit -n 2048` in the same shell | ||
that will run the io.js process. | ||
|
||
#### EPIPE: Broken pipe | ||
|
||
A write on a pipe, socket, or FIFO for which there is no process to read the | ||
data. Commonly encountered at the [net](net.html) and [http](http.html) layers, indicative that | ||
the remote side of the stream being written to has been closed. | ||
|
||
#### EADDRINUSE: Address already in use | ||
|
||
An attempt to bind a server ([net](net.html), [http](http.html), or [https](https.html)) to a local | ||
address failed due to another server on the local system already occupying | ||
that address. | ||
|
||
#### ECONNRESET: Connection reset by peer | ||
|
||
A connection was forcibly closed by a peer. This normally results | ||
from a loss of the connection on the remote socket due to a timeout | ||
or reboot. Commonly encountered via the [http](http.html) and [net](net.html) modules. | ||
|
||
#### ECONNREFUSED: Connection refused | ||
|
||
No connection could be made because the target machine actively refused | ||
it. This usually results from trying to connect to a service that is inactive | ||
on the foreign host. | ||
|
||
#### ENOTEMPTY: Directory not empty | ||
|
||
A directory with entries was the target of an operation that requires | ||
an empty directory -- usually [fs.unlink](fs.html#fs_fs_unlink_path_callback). | ||
|
||
#### ETIMEDOUT: Operation timed out | ||
|
||
A connect or send request failed because the connected party did not properly | ||
respond after a period of time. Usually encountered by [http](http.html) or [net](net.html) -- | ||
often a sign that a connected socket was not `.end()`'d appropriately. | ||
|
||
## Error Propagation and Interception | ||
|
||
<!--type=misc--> | ||
|
||
All io.js APIs will treat invalid arguments as exceptional -- that is, if passed | ||
invalid arguments, they will *immediately* generate and throw the error as an | ||
exception, even if they are an otherwise asynchronous API. | ||
|
||
Synchronous APIs (like | ||
[fs.readFileSync](fs.html#fs_fs_readfilesync_filename_options)) will throw the | ||
error. The act of *throwing* a value (in this case, the error) turns the value | ||
into an **exception**. Exceptions may be caught using the `try { } catch(err) | ||
{ }` construct. | ||
|
||
Asynchronous APIs have **two** mechanisms for error propagation; one mechanism | ||
for APIs that represent a single operation, and one for APIs that represent | ||
multiple operations over time. | ||
|
||
### Node style callbacks | ||
|
||
<!--type=misc--> | ||
|
||
Single operation APIs take "node style callbacks" -- a | ||
function provided to the API as an argument. The node style callback takes | ||
at least **one** argument -- `error` -- that will either be `null` (if no error | ||
was encountered) or an `Error` instance. For instance: | ||
|
||
```javascript | ||
var fs = require('fs'); | ||
|
||
fs.readFile('/some/file/that/does-not-exist', function nodeStyleCallback(err, data) { | ||
console.log(err) // Error: ENOENT | ||
console.log(data) // undefined / null | ||
}); | ||
|
||
fs.readFile('/some/file/that/does-exist', function(err, data) { | ||
console.log(err) // null | ||
console.log(data) // <Buffer: ba dd ca fe> | ||
}) | ||
``` | ||
|
||
Note that `try { } catch(err) { }` **cannot** intercept errors generated by | ||
asynchronous APIs. A common mistake for beginners is to try to use `throw` | ||
inside their node style callback: | ||
|
||
```javascript | ||
// THIS WILL NOT WORK: | ||
var fs = require('fs'); | ||
|
||
try { | ||
fs.readFile('/some/file/that/does-not-exist', function(err, data) { | ||
// mistaken assumption: throwing here... | ||
if (err) { | ||
throw err; | ||
} | ||
}); | ||
} catch(err) { | ||
// ... will be caught here -- this is incorrect! | ||
console.log(err); // Error: ENOENT | ||
} | ||
``` | ||
|
||
This will not work! By the time the node style callback has been called, the | ||
surrounding code (including the `try { } catch(err) { }` will have already | ||
exited. Throwing an error inside a node style callback **will crash the process** in most cases. | ||
If [domains](domain.html) are enabled, they may intercept the thrown error; similarly, if a | ||
handler has been added to `process.on('uncaughtException')`, it will intercept | ||
the error. | ||
|
||
### Error events | ||
|
||
<!--type=misc--> | ||
|
||
The other mechanism for providing errors is the "error" event. This is | ||
typically used by [stream-based](stream.html) and [event emitter-based](events.html#events_class_events_eventemitter) APIs, which | ||
themselves represent a series of asynchronous operations over time (versus a | ||
single operation that may pass or fail). If no "error" event handler is | ||
attached to the source of the error, the error will be thrown. At this point, | ||
it will crash the process as an unhandled exception unless [domains](domain.html) are | ||
employed appropriately or [process.on('uncaughtException')](process.html#process_event_uncaughtexception) has a handler. | ||
|
||
```javascript | ||
var net = require('net'); | ||
|
||
var connection = net.connect('localhost'); | ||
|
||
// adding an "error" event handler to a stream: | ||
connection.on('error', function(err) { | ||
// if the connection is reset by the server, or if it can't | ||
// connect at all, or on any sort of error encountered by | ||
// the connection, the error will be sent here. | ||
console.error(err); | ||
}); | ||
|
||
connection.pipe(process.stdout); | ||
``` | ||
|
||
The "throw when no error handlers are attached behavior" is not limited to APIs | ||
provided by io.js -- even user created event emitters and streams will throw | ||
errors when no error handlers are attached. An example: | ||
|
||
```javascript | ||
var events = require('events'); | ||
|
||
var ee = new events.EventEmitter; | ||
|
||
setImmediate(function() { | ||
// this will crash the process because no "error" event | ||
// handler has been added. | ||
ee.emit('error', new Error('This will crash')); | ||
}); | ||
``` | ||
|
||
As with node style callbacks, errors generated this way *cannot* be intercepted | ||
by `try { } catch(err) { }` -- they happen *after* the calling code has already | ||
exited. |