question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature suggestion: default replacer to better handle Error objects passed as meta

See original GitHub issue

Hi, thanks a bunch for this project. Super helpful.

Right now, when passing Error objects in the meta parameter they get logged as {} because of the design of the Error object (as you know doubt know).

const log = require('lambda-log')
const error = new Error('foo')
log.warn('Heads up!', {error})
// {"_logLevel":"warn","msg":"Heads up!","error":{},"_tags":["log","warn"]}

I can use log.options.replacer to get a much better serialization of the Error object (using the error-to-json package here):

const log = require('lambda-log')
const errorToJson = require('error-to-json').default
log.options.replacer = (key, value) => {
  if (value instanceof Error) return errorToJson(value)
  return value
}

const error = new Error('foo')
log.warn('Heads up!', {error})
// {"_logLevel":"warn","msg":"Heads up!","error":{"name":"Error","message":"foo","stack":"Error: foo\n    at REPL9:1:15\n    at Script.runInThisContext (vm.js:120:18)\n    at REPLServer.defaultEval (repl.js:442:29)\n    at bound (domain.js:427:14)\n    at REPLServer.runBound [as eval] (domain.js:440:12)\n    at REPLServer.onLine (repl.js:777:10)\n    at REPLServer.emit (events.js:326:22)\n    at REPLServer.EventEmitter.emit (domain.js:483:12)\n    at REPLServer.Interface._onLine (readline.js:329:10)\n    at REPLServer.Interface._line (readline.js:658:8)"},"_tags":["log","warn"]}

My suggestion is to consider making this the default behavior. Are there any use cases where serializing an Error to {} would be the better behavior?

Thanks again for the project!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
KyleRosscommented, Apr 15, 2021

@all-contributors please add @mfogel for ideas

1reaction
KyleRosscommented, Feb 15, 2021

@mfogel Right now, this package will handle Error objects correctly when passed in as the message versus within the meta. I can see the use cases where you may want to pass an error in the meta instead.

I’m trying to keep the number of dependencies within this package as minimal as possible so I think I would like to recreate the functionality of the error-to-json package inside of this one given it’s a minimal amount of code.

I’ll work on adding this in the next release. Thanks for the feature request!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.js Best Practice Exception Handling - Stack Overflow
Error objects, as we'll see, have very interesting properties in modern JavaScript engines besides holding the message passed to the constructor.." ...
Read more >
Handle errors in ASP.NET Core web APIs - Microsoft Learn
This article describes how to handle errors and customize error handling with ASP.NET Core web APIs. Developer Exception Page.
Read more >
Working with Rules - ESLint - Pluggable JavaScript Linter
"suggestion" means the rule is identifying something that could be done in a better way but no errors will occur if the code...
Read more >
Best Practices for Node.js Error-handling - Toptal
This article will introduce you to error-handling in Node.js and demonstrate ... Using Node.js built-in Error object is a good practice because it...
Read more >
Control flow and error handling - JavaScript - MDN Web Docs
If a default clause is found, the program transfers control to that ... to handle any exceptions logMyErrors(err); // pass exception object ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found