Code Monkey home page Code Monkey logo

log-process-errors's Introduction

log-process-errors logo

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.

Hire me

Please reach out if you're looking for a Node.js API or CLI engineer (11 years of experience). Most recently I have been Netlify Build's and Netlify Plugins' technical lead for 2.5 years. I am available for full-time remote positions.

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

🐛

log-process-errors's People

Contributors

allcontributors[bot] avatar dependabot[bot] avatar ehmicky avatar hong4rc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

log-process-errors's Issues

[BUG] Not compatible with Node 6

Describe the bug

Package isn't compiled down to ES6.

[root@machine]# node -r log-process-errors/build/register dist/index.min.js
./node_modules/log-process-errors/build/src/options/main.js:24
const optsC={...DEFAULT_OPTS,...optsB,level};
             ^^^

SyntaxError: Unexpected token ...
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:549:28)
    at Object.Module._extensions..js (module.js:586:10)
    at Module.load (module.js:494:32)
    at tryModuleLoad (module.js:453:12)
    at Function.Module._load (module.js:445:3)
    at Module.require (module.js:504:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/opt/exten/ms-volume/node_modules/log-process-errors/build/src/main.js:6:11)

6.1.0 - Errors when ExperimentalWarning is caught

Describe the bug

Error in 6.1.0 when an ExperimentalWarning is caught:

TypeError: Cannot read property 'stack' of undefined
    at message (node_modules/log-process-errors/src/error/stack.js:4:37)
    at getError (node_modules/log-process-errors/src/error/main.js:16:36)
    at logEvent (node_modules/log-process-errors/src/handle/common.js:42:39)
    at handleEvent (node_modules/log-process-errors/src/handle/common.js:36:9)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

Steps to reproduce

Have this module process an ExperimentalWarning (maybe others). In my case it was:

ExperimentalWarning: Conditional exports is an experimental feature. This feature could change at any time
    at emitExperimentalWarning (internal/util.js:183:11)
    at resolveExportsTarget (internal/modules/cjs/loader.js:607:13)
    at applyExports (internal/modules/cjs/loader.js:500:14)
    at resolveExports (internal/modules/cjs/loader.js:549:12)
    at Function.Module._findPath (internal/modules/cjs/loader.js:664:22)
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:967:27)
    at Object._resolveFilename (/node_modules/rewiremock/lib/module.js:60:32)
    at /node_modules/rewiremock/lib/executor.js:269:29
    at tryOr (/node_modules/rewiremock/lib/executor.js:224:12)
    at Function.mockLoader (/node_modules/rewiremock/lib/executor.js:268:21) {
  name: 'ExperimentalWarning'
}

Don't have time for additional info right at this moment, sorry!

Casing change in 10.0.0 release notes

Hello, I was reading the 10.0.0 release notes and noticed that the "before" code uses "UncaughtException" while the "after" code uses "uncaughtException" instead (changed from PascalCase to camelCase). Why? I think it would be good to have that better described in the release notes. Or is it a typo? Thanks!

Why not raise the core console.log problems to node.js?

Fantastic, totally dig the idea. There is too much noise in console.log but why another new library? Raise this with the node.js core community and improve it there instead?

Sorry for the blunt question, but I feel like I have to ... Peace out :)

5.1.0 marked as latest in npm

Not sure how it happened, but 5.1.0 is marked as latest in npm, above 6.1.0, this caused yarn upgrade --latest to attempt to downgrade the package:

image

Remove (seemingly) unused core-js dependency

Which problem is this feature request solving?

Would rather not involuntary use core-js

Describe the solution you'd like

Remove core-js from package.json and package-lock.json

Describe alternatives you've considered

Leaving it be, if log-process-errors uses it somehow I'm not seeing

Can you submit a pull request?

Yes.

Seeing I only get one result when searching for "core-js" (https://github.com/ehmicky/log-process-errors/search?q=core-js&unscoped_q=core-js) - is it perhaps not used anymore?

`tape` is loaded even without the `testing: tape` option

Describe the bug

When using log-process-errors, the following error is being thrown:

ERROR in /my-project/node_modules/log-process-errors/build/src/options/runners.js
Module not found: Error: Can't resolve 'tape' in '/my-project/node_modules/log-process-errors/build/src/options'
 @ /my-project/node_modules/log-process-errors/build/src/options/runners.js 17:11-26
 @ /my-project/node_modules/log-process-errors/build/src/options/testing.js
 @ /my-project/node_modules/log-process-errors/build/src/options/main.js
 @ /my-project/node_modules/log-process-errors/build/src/main.js
 @ my-file.js

As a workaround, I installed tape as a dependency of my project which solved the issue.

My environment is a little bit special, as it is a Yarn workspaces project, but as far as I can see that is not relevant to the issue.

Also note that I am using the netlify-lambda CLI to compile my-file.js, which uses webpack under the hood (not sure how its config looks) and using ES6 import syntax to load log-process-errors.

Steps to reproduce

Not sure, as I don't know which component(s) of my rather exotic setup are causing the issue.

Environment

  System:
    OS: Linux 5.0 Manjaro Linux
    CPU: (8) x64 Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
    Memory: 1.41 GB / 15.39 GB
    Container: Yes
    Shell: 3.0.2 - /usr/bin/fish
  Binaries:
    Node: 11.14.0 - /usr/bin/node
    Yarn: 1.15.2 - /usr/bin/yarn
    npm: 6.9.0 - /usr/bin/npm

Can you submit a pull request?

Yes, but it may take a while. When I have more time, I will first try to put together a minimal reproduction demo and then see how the issue can be fixed.

How do I use this to log an error that I have already caught?

I have an error variable that I caught from somewhere else. I would like to log it cleanly to console just like this library does. However, from the readme, it looks like this library only acts on uncaught errors. Does this library also provide a way to do this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.