Skip to content

Commit cea8dc5

Browse files
committed
Add examples
1 parent d9eb61a commit cea8dc5

File tree

11 files changed

+217
-10
lines changed

11 files changed

+217
-10
lines changed

README.md

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ With `log-process-errors`:
4040
- **Debugging** of process errors in development.
4141
- Automated **testing** of process errors.
4242

43+
# Demo
44+
45+
You can try this library:
46+
47+
- either directly
48+
[in your browser](https://repl.it/@ehmicky/log-process-errors).
49+
- or by executing the [`examples` files](examples/README.md) in a terminal.
50+
4351
# Install
4452

4553
Production code (e.g. a server) can install this either as a production or
@@ -80,43 +88,43 @@ logProcessErrors(options)
8088

8189
# Options
8290

83-
`options` is an optional object with the following properties.
84-
85-
This is a quick summary. Please see the
86-
[options full documentation](docs/API.md) for more information and examples.
87-
88-
## [log](docs/API.md#log)
91+
## log
8992

9093
_Type_: `function(message, level, event)`<br>
9194

9295
Customize how events are logged.
96+
[Full documentation](docs/API.md#log).
9397

94-
## [level](docs/API.md#level)
98+
## level
9599

96100
_Type_: `object`<br>
97101
_Default_: `{ warning: 'warn', multipleResolves: 'info', default: 'error' }`
98102

99103
Which log level to use.
104+
[Full documentation](docs/API.md#level).
100105

101-
## [message](docs/API.md#message)
106+
## message
102107

103108
_Type_: `function(level, event, options) => string`
104109

105110
Customize messages.
111+
[Full documentation](docs/API.md#message).
106112

107-
## [colors](docs/API.md#colors)
113+
## colors
108114

109115
_Type_: `boolean`<br>
110116
_Default_: `true` if the output is a terminal.
111117

112118
Colorize messages.
119+
[Full documentation](docs/API.md#colors).
113120

114-
## [exitOn](docs/API.md#exiton)
121+
## exitOn
115122

116123
_Type_: `string[]`<br>
117124
_Default_: `["uncaughtException"]`
118125

119126
Which events should trigger `process.exit(1)`.
127+
[Full documentation](docs/API.md#exiton).
120128

121129
# Support
122130

docs/API.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# API
22

3+
You can try all the examples below:
4+
5+
- either directly
6+
[in your browser](https://repl.it/@ehmicky/log-process-errors).
7+
- or by executing the [`examples` files](../examples/README.md) in a terminal.
8+
39
## logProcessErrors([options])
410

511
Initialize `log-process-errors`. Returns a function that can be fired to restore

examples/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
This directory contains examples of this library.
2+
3+
To execute them, first [install](../README.md#install) `log-process-errors`.
4+
Then:
5+
6+
- for JavaScript files, run
7+
`node node_modules/log-process-errors/examples/FILE.js`.
8+
- for command line files (Bash), run
9+
`bash node_modules/log-process-errors/examples/FILE.sh`.
10+
11+
You can edit the examples.
12+
13+
They can also be run directly
14+
[in your browser](https://repl.it/@ehmicky/log-process-errors).
15+
16+
## Main usage
17+
18+
- Process errors without `log-process-errors`:
19+
[JavaScript](before.js), [command line](before.sh).
20+
- Process errors with `log-process-errors`:
21+
[JavaScript](after.js), [command line](after.sh).

examples/after.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env node
2+
// Demo of process errors when `log-process-errors` is used in JavaScript.
3+
// This file can be directly run:
4+
// - first install `log-process-errors`
5+
// - then `node node_modules/log-process-errors/examples/after.js`
6+
// An online demo is also available at:
7+
// https://repl.it/@ehmicky/log-process-errors
8+
9+
'use strict'
10+
11+
// Ignore the following line: this is only needed for internal purposes.
12+
// eslint-disable-next-line import/no-unassigned-import
13+
require('./utils')
14+
15+
const logProcessErrors = require('log-process-errors')
16+
17+
const {
18+
uncaughtException,
19+
unhandledRejection,
20+
warning,
21+
multipleResolves,
22+
} = require('./errors')
23+
24+
// Initialization
25+
logProcessErrors()
26+
27+
// Emit different types of process errors.
28+
uncaughtException()
29+
unhandledRejection()
30+
warning()
31+
multipleResolves()

examples/after.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
# Demo of process errors when `log-process-errors` is used in the terminal
3+
# (Bash).
4+
# This file can be directly run:
5+
# - first install `log-process-errors`
6+
# - then `bash node_modules/log-process-errors/examples/after.sh`
7+
# An online demo is also available at:
8+
# https://repl.it/@ehmicky/log-process-errors
9+
10+
# Ignore the following line: this is only needed for internal purposes.
11+
. "$(dirname "$BASH_SOURCE")/utils.sh"
12+
13+
node -r log-process-errors/register "$dir/before.js"

examples/before.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env node
2+
// Demo of process errors when `log-process-errors` is not used in JavaScript.
3+
// This file can be directly run:
4+
// - first install `log-process-errors`
5+
// - then `node node_modules/log-process-errors/examples/before.js`
6+
// An online demo is also available at:
7+
// https://repl.it/@ehmicky/log-process-errors
8+
9+
'use strict'
10+
11+
const {
12+
uncaughtException,
13+
unhandledRejection,
14+
warning,
15+
multipleResolves,
16+
} = require('./errors')
17+
18+
// Emit different types of process errors.
19+
uncaughtException()
20+
unhandledRejection()
21+
warning()
22+
multipleResolves()

examples/before.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
# Demo of process errors when `log-process-errors` is not used in the terminal
3+
# (Bash).
4+
# This file can be directly run:
5+
# - first install `log-process-errors`
6+
# - then `bash node_modules/log-process-errors/examples/after.sh`
7+
# An online demo is also available at:
8+
# https://repl.it/@ehmicky/log-process-errors
9+
10+
# Ignore the following line: this is only needed for internal purposes.
11+
. "$(dirname "$BASH_SOURCE")/utils.sh"
12+
13+
node "$dir/before.js"

examples/errors.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// The methods in this file trigger different types of process errors.
2+
// This is done for example purposes.
3+
'use strict'
4+
5+
const { emitWarning } = require('process')
6+
7+
// Emit an `uncaughtException` event
8+
const uncaughtException = function() {
9+
setTimeout(() => {
10+
// eslint-disable-next-line max-nested-callbacks
11+
setTimeout(() => {
12+
throw new Error('File not found')
13+
}, 0)
14+
}, 0)
15+
}
16+
17+
// Emit `unhandledRejection` and `rejectionHandled` events
18+
const unhandledRejection = function() {
19+
const promise = Promise.reject(new Error('Invalid permission'))
20+
setTimeout(() => {
21+
// eslint-disable-next-line no-empty-function, max-nested-callbacks
22+
promise.catch(() => {})
23+
}, 0)
24+
}
25+
26+
// Emit a `warning` event
27+
const warning = function() {
28+
emitWarning('Invalid option', {
29+
type: 'DeprecationWarning',
30+
code: 'DeprecatedMethod',
31+
detail: 'opts.force is deprecated',
32+
})
33+
}
34+
35+
// Emit a `multipleResolves` event
36+
const multipleResolves = function() {
37+
// eslint-disable-next-line no-new, promise/avoid-new
38+
new Promise((resolve, reject) => {
39+
resolve({ success: true })
40+
reject(new Error('Cannot send request'))
41+
})
42+
}
43+
44+
module.exports = {
45+
uncaughtException,
46+
unhandledRejection,
47+
warning,
48+
multipleResolves,
49+
}

examples/utils.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Ignore this file, this is only needed for internal purposes.
2+
// We mock `require()` so that examples look the same as if the library was
3+
// directly installed.
4+
5+
'use strict'
6+
7+
const Module = require('module')
8+
9+
const { name } = require('../package')
10+
11+
const originalRequire = Module.prototype.require
12+
13+
// eslint-disable-next-line fp/no-mutation, func-names
14+
Module.prototype.require = function(moduleName, ...args) {
15+
const moduleNameA = getMockedName(moduleName)
16+
// eslint-disable-next-line fp/no-this
17+
return originalRequire.call(this, moduleNameA, ...args)
18+
}
19+
20+
const getMockedName = function(moduleName) {
21+
if (moduleName !== name && !moduleName.startsWith(`${name}/`)) {
22+
return moduleName
23+
}
24+
25+
return moduleName.replace(name, `${__dirname}/..`)
26+
}

examples/utils.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bash
2+
# Ignore this file, this is only needed for internal purposes.
3+
4+
dir="$(dirname "$BASH_SOURCE")"
5+
projectRoot="$(realpath "$dir/..")"
6+
binaryName="$(basename "$projectRoot")"
7+
pathToBinary="build/src/bin/index.js"
8+
9+
shopt -s expand_aliases
10+
11+
# We create an alias so that examples look the same as if the library was
12+
# directly installed.
13+
alias "$binaryName"="$projectRoot/$pathToBinary"
14+
15+
# This mocks node's `-r` flag so that examples look the same as if the library
16+
# directly installed
17+
alias node="node -r $dir/utils"

0 commit comments

Comments
 (0)