包详细信息

log-process-errors

ehmicky636.9kApache-2.012.0.1

Show some ❤️ to process errors

code-quality, debugging, error, error-handler

自述文件

<picture> <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/ehmicky/design/main/log-process-errors/log-process-errors_dark.svg"/> log-process-errors logo </picture>

Node TypeScript Codecov Mastodon Medium

📰 Medium article.

Show some ❤️ to Node.js process errors.

This improves process errors: uncaught exceptions, unhandled promises, promises handled too late and warnings.

Features

Install

Production code (e.g. a server) can install this either as a production or development dependency:

npm install log-process-errors

However, libraries should install this as a development dependency:

npm install -D log-process-errors

This is because logging is modified globally and libraries users might not expect this side-effect. Also, this might lead to conflicts between libraries.

This package works in Node.js >=18.18.0.

This is an ES module. It must be loaded using an import or import() statement, not require(). If TypeScript is used, it must be configured to output ES modules, not CommonJS.

API

logProcessErrors(options?)

options object?\ Return value: () => void

Start handling process errors.

import logProcessErrors from 'log-process-errors'
logProcessErrors(options)

The return value restores Node.js default behavior.

const restore = logProcessErrors(options)
restore()

Options

exit

Type: boolean

Whether to exit the process on uncaught exceptions or unhandled promises.

This is false by default if other libraries are listening to those events, so they can perform the exit instead. Otherwise, this is true.

If some tasks are still ongoing, the exit waits for them to complete up to 3 seconds.

onError

Type: (error, event) => Promise<void> | void\ Default: console.error(error)

Function called once per process error. Duplicate process errors are ignored.

// Log process errors with Winston instead
logProcessErrors({
  onError: (error, event) => {
    winstonLogger.error(error.stack)
  },
})

error

Type: Error

The process error. This is guaranteed to be a normalized error instance. A short description of the event is also appended to its message.

event

Type: Event

Process event name among: 'uncaughtException', 'unhandledRejection', 'rejectionHandled', 'warning'.

Related projects

Support

For any question, don't hesitate to submit an issue on GitHub.

Everyone is welcome regardless of personal background. We enforce a Code of conduct in order to promote a positive and inclusive environment.

Contributing

This project was made with ❤️. The simplest way to give back is by starring and sharing it online.

If the documentation is unclear or has a typo, please click on the page's Edit button (pencil icon) and suggest a correction.

If you would like to help us fix a bug or add a new feature, please check our guidelines. Pull requests are welcome!

Thanks go to our wonderful contributors:


ehmicky

💻 🎨 🤔 📖

Steven Vachon

💬

Hongarc

📖 💻

Andy Brenneke

🐛

更新日志

12.0.1

Documentation

  • Improve documentation in README.md

12.0.0

Breaking changes

  • Minimal supported Node.js version is now 18.18.0

11.0.1

Dependencies

  • Upgrade internal dependencies

11.0.0

Breaking changes

  • Minimal supported Node.js version is now 16.17.0

10.2.0

Features

  • Improve tree-shaking support

10.1.1

Internal

  • Add more tests

10.1.0

Features

10.0.0

Package size

The npm package size has been reduced by 98%, from 4500kB to 87kB.

Custom logic

The log option was renamed to onError. Its arguments are (originalError, event) instead of (error, level, originalError).

The process error event is now passed as a second argument instead of being set as error.name. Its case is not capitalized anymore, to match the event name in Node.js.

Before:

logProcessErrors({
  log: (error) => {
    if (error.name === 'UncaughtException') {
      console.error(error)
    }
  },
})

After:

logProcessErrors({
  onError: (error, event) => {
    if (event === 'uncaughtException') {
      console.error(error)
    }
  },
})

Pretty-printing

Errors are not pretty-printed anymore. As a consequence, the colors option was removed too. The onError option can be used instead to customize how the errors are printed.

Filtering

The levels option was removed. The onError option can be used for filtering.

Before:

logProcessErrors({
  levels: {
    warning: 'silent',
  },
})

After:

logProcessErrors({
  onError: (error, event) => {
    if (event !== 'warning') {
      console.error(error)
    }
  },
})

Testing option

The testing option was removed. The onError option can be used instead.

Before:

logProcessErrors({ testing: 'ava' })

After:

logProcessErrors({
  // Throw the `error` to make the unit test fail while letting other tests run
  onError: (error) => {
    throw error
  },
})

Process exit

The exitOn option was changed from an array of strings to a simpler boolean. It was also renamed exit.

The exit is still graceful, i.e. it waits for ongoing tasks to complete, up to 3 seconds. However, if there are none, the process now exits immediately.

Before:

logProcessErrors({ exitOn: [] })

After:

logProcessErrors({ exit: false })

Compatibility with other libraries

If other libraries (such as Winston, Bugsnag, etc.) are also listening for process events, they might also try to exit the process. This created conflicts with this library. This has been fixed by making the exit option default to false when process events listeners already exist.

Bug fixes

  • Fix support for --unhandled-rejections=strict
  • Do not crash when error.stack is undefined or null
  • Support cross-realm errors

TypeScript

TypeScript types have been simplified.

Internal

Added 100% test coverage.

9.4.0

Features

  • Reduce npm package size

9.3.0

Documentation

9.2.0

Features

  • Reduce npm package size

9.1.0

Features

  • Add TypeScript types

9.0.0

Breaking changes

  • Minimal supported Node.js version is now 14.18.0

8.0.0

Breaking changes

  • multipleResolves has been deprecated by Node.js. Therefore, support for it has been removed.
    • If your code uses the level or exitOn option with a multipleResolves parameter, you should remove it.
    • Otherwise, this release is not a breaking change for you.

7.0.1

Bug fixes

  • Fix main field in package.json

7.0.0

Breaking changes

  • The log-process-errors/build/register export has been removed. Please import log-process-errors directly instead.
  • The testing option value node-tap has been renamed to node_tap
  • Minimal supported Node.js version is now 12.20.0
  • This package is now an ES module. It can only be loaded with an import or import() statement, not require(). See this post for more information.

Features

  • Improve colors detection

6.3.0

Features

  • Improve colors on Windows terminal

6.2.0

Features

  • The exitOn() option now defaults to ['uncaughtException', 'unhandledRejection'] on Node >= 15.0.0. Its default value is still ['uncaughtException'] on Node < 15.0.0. This is to mimic Node.js default behavior: since Node 15.0.0, processes exit on unhandled promises.

6.1.1

Bug fixes

  • Fix handling uncaught exceptions or warnings that are not Error instances (#32)

6.1.0

Features

  • Pass original error to log() option

Bug fixes

  • Do not remove error static properties

6.0.1

Dependency

  • Remove unused dependency core-js

6.0.0

Breaking changes

  • Minimal supported Node.js version is now 10.17.0

5.1.2 (backport)

Dependency

  • Remove unused dependency core-js

5.1.1 (backport)

Bug fixes

  • Fix handling uncaught exceptions or warnings that are not Error instances (#32)

5.1.0 (backport)

Features

  • Pass original error to log() option

Bug fixes

  • Do not remove error static properties

5.0.3

Bug fixes

  • Do not make tests fail on multipleResolves events with the testing option

5.0.2

Bug fixes

  • Do not handle deprecation warnings on unhandled rejection promises anymore since this is due to a bug in Node 12.6.0 which has been fixed in 12.7.0.

5.0.1

Bug fixes

  • Do not print deprecation warnings on unhandled rejection promises with Node 12.6.0

Dependencies

  • Upgrade supports-color

5.0.0

Thanks to @Hongarc and @stevenvachon for their first-time contributions!

Breaking changes

  • Minimum Node.js version is now 8.12.0 (instead of 8.10.0)

Documentation

  • Improve documentation and examples

Internal

  • Upgrade development dependencies
  • Code refactoring
  • Improve linting

4.1.1

Dependencies

  • Upgrade figures
  • Upgrade moize

Internal

  • Upgrade development dependencies
  • Code refactoring

4.1.0

Internal

  • Use ES modules

4.0.0

Breaking changes

  • Rename log-process-errors/register to log-process-errors/build/register