eslint-plugin-security-node
Version:
Create a security plugin for node.js
30 lines (23 loc) • 1.15 kB
Markdown
When using the EventEmitter, it cannot be denied that errors can occur anywhere in the chain.
Given this code below:
```javascript
const EventEmitter = require('events')
const myEmitter = new EventEmitter()
myEmitter.emit('error', new Error('whoops!'))
// Throws and crashes Node.js
```
Since there is no error event listener called, the error will be thrown and will be treated as an uncaughtException. Once an error is emitted and unhandled, the Node.js application will crash.
There should always be listeners for events at the least. This of course does not count out error events.
```javascript
const EventEmitter = require('events')
const myEmitter = new EventEmitter()
// MUST: include error listener
myEmitter.on('error', (err) => {
console.error('An error occurred')
})
myEmitter.emit('error', new Error('whoops!'))
// Prints: An error occurred
```
[](https://nodejs.org/dist/latest-v16.x/docs/api/events.html#error-events)
[](https://cheatsheetseries.owasp.org/cheatsheets/Nodejs_Security_Cheat_Sheet.html#listen-to-errors-when-using-eventemitter)