Skip to content

Feature request: Include cause in stack trace #1361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 of 2 tasks
everett1992 opened this issue Mar 9, 2023 · 9 comments · Fixed by #1617
Closed
1 of 2 tasks

Feature request: Include cause in stack trace #1361

everett1992 opened this issue Mar 9, 2023 · 9 comments · Fixed by #1617
Assignees
Labels
completed This item is complete and has been merged/shipped feature-request This item refers to a feature request for an existing or new utility logger This item relates to the Logger Utility

Comments

@everett1992
Copy link

Use case

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause

The cause data property of an Error instance indicates the specific original cause of the error.

Error's cause property is a useful way to provide extra information about an error, particularly because it is a structured object, rather than adding to the string message.

However powertools does not include cause in log messages

import { Logger } from '@aws-lambda-powertools/logger'
const err = new Error('message', { cause : new Error('cause') })
console.log(err)
new Logger().error("err", err)
Error: message
    at file://test.mjs:2:13
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    ... 3 lines matching cause stack trace ...
    at async handleMainPromise (node:internal/modules/run_main:65:12) {
  [cause]: Error: cause
      at file://test.mjs:2:44
      at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
      at async Promise.all (index 0)
      at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
      at async loadESM (node:internal/process/esm_loader:91:5)
{
    "level": "ERROR",
    "message": "err",
    "service": "service_undefined",
    "timestamp": "2023-03-09T19:27:32.538Z",
    "error": {
        "name": "Error",
        "location": "node:internal/modules/esm/module_job:193",
        "message": "message",
        "stack": "Error: message\n    at file://test.mjs:2:13\n    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)\n    at async Promise.all (index 0)\n    at async ESMLoader.import (node:internal/modules/esm/loader:530:24)\n    at async loadESM (node:internal/process/esm_loader:91:5)\n    at async handleMainPromise (node:internal/modules/run_main:65:12)"
    }
}

Solution/User Experience

powertools should include cause when it is defined.

Alternative solutions

No response

Acknowledgment

Future readers

Please react with 👍 and your use case to help us understand customer demand.

@everett1992 everett1992 added triage This item has not been triaged by a maintainer, please wait feature-request This item refers to a feature request for an existing or new utility labels Mar 9, 2023
@dreamorosi dreamorosi added logger This item relates to the Logger Utility confirmed The scope is clear, ready for implementation and removed triage This item has not been triaged by a maintainer, please wait labels Mar 9, 2023
@dreamorosi
Copy link
Contributor

Hi @everett1992, thank you for opening this issue.

I agree with you that having the cause in the logs when there's an error would be beneficial and provide additional context to users.

I'm going to add this to the backlog and also mark it as help-wanted / good-first-issue to signal that the issue is open for contributions.

If anyone is interested in picking this up, please leave a comment below to agree on the scope with a maintainer and ask any question before starting to work on a PR.

@dreamorosi dreamorosi added good-first-issue Something that is suitable for those who want to start contributing help-wanted We would really appreciate some support from community for this one labels Mar 9, 2023
@dVp007
Copy link

dVp007 commented Mar 10, 2023

Hello @dreamorosi I would like to work on the PR.

@dreamorosi dreamorosi added blocked This item's progress is blocked by external dependency or reason and removed help-wanted We would really appreciate some support from community for this one confirmed The scope is clear, ready for implementation good-first-issue Something that is suitable for those who want to start contributing labels Mar 10, 2023
@dreamorosi
Copy link
Contributor

Hi @dVp007, thank you for offering to work on this issue.

Unfortunately after looking into it a bit more, I don't think we can support the feature just yet. The cause field was added in Node.js starting from version v16.9.0 (source), and at the moment we are committed to offer feature parity for versions 14.x through 18.x, which means we can't offer this feature for the time being.

I should have checked this better before marking the feature as status/confirmed, apologies for the confusion. I'm going to mark this as status/blocked and we'll revisit this once we decide to drop support for Node.js 14.x. We don't have an ETA to share at the moment but it'll likely happen sometime after Lambda will announce its deprecation or EOL.


If you're still interested in contributing, I'd encourage you to look for other issues marked as good-first-issue. Thank you for your understanding.

@dreamorosi dreamorosi moved this from Backlog to On hold in AWS Lambda Powertools for TypeScript Mar 10, 2023
@everett1992
Copy link
Author

everett1992 commented Mar 10, 2023 via email

@mistaecko
Copy link

@dreamorosi Can you please elaborate on your reasoning not to support error cause. This language feature is a major improvement to error handling and logging, and the decision not to support it in Powertools is quite surprising.

If user code targets ES2022 and supporting node versions (>= 16.9.0), and logs errors with error cause property, I would really expect the logger to print the additional information (just as the native console.log would). This is not merely a convenience, but rather crucial for obtaining necessary information for troubleshooting.

Powertools is preventing us from using a modern language feature. Just as with other language features, there is no expectation for Powertools to polyfill this feature for older node versions.
That said, it should be easy enough for user code to install a shim, and any implementation in Powertools that inspects errror.cause should work just as well.

@niko-achilles
Copy link
Contributor

i am also interested in this feature of supporting the cause property in logs . If that helps to prioritise.

@dreamorosi dreamorosi self-assigned this Jul 14, 2023
@dreamorosi
Copy link
Contributor

Hi both, thank you for taking the time to show interest in this feature - this definitely helps us with prioritization and to understand demand.

My initial push back towards adding the feature came from wanting to offer a cohesive Powertools experience across runtimes. Since the issue was opened however there seems to be a lot more demand that I anticipated so I'm more than open to reconsider.

On a general level I'm not keen on introducing a polyfill and after an initial proof of concept, the approach suggested by @everett1992 here, seems to be enough.

Given that the Node API doesn't seem to specify any strong type/value for cause, and given that in JavaScript you can throw other values other than Error I'm thinking on supporting the following three cases:

  1. Logging an error with cause of Error type, formats both the error and the cause as errors:
logger.error(
  "something went wrong",
  new Error("boom", { cause: new Error("foo") })
);
// prints
{
    "level": "ERROR",
    "message": "something went wrong",
    "service": "service_undefined",
    "timestamp": "2023-07-14T11:53:06.944Z",
    "error": {
        "name": "Error",
        "location": "/Users/aamorosi/Codes/error-cause/index.ts:8",
        "message": "boom",
        "stack": "Error: boom\n    at Object.<anonymous> (/Users/aamorosi/Codes/error-cause/index.ts:8:3)\n    at Module._compile (node:internal/modules/cjs/loader:1159:14)\n    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)\n    at Module.load (node:internal/modules/cjs/loader:1037:32)\n    at Module._load (node:internal/modules/cjs/loader:878:12)\n    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)\n    at node:internal/main/run_main_module:23:47",
        "cause": {
            "name": "Error",
            "location": "/Users/aamorosi/Codes/error-cause/index.ts:8",
            "message": "foo",
            "stack": "Error: foo\n    at Object.<anonymous> (/Users/aamorosi/Codes/error-cause/index.ts:8:30)\n    at Module._compile (node:internal/modules/cjs/loader:1159:14)\n    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)\n    at Module.load (node:internal/modules/cjs/loader:1037:32)\n    at Module._load (node:internal/modules/cjs/loader:878:12)\n    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)\n    at node:internal/main/run_main_module:23:47"
        }
    }
}

2 Logging an error with cause of types other than Error (i.e. string) formats the error and appends the cause as-is.

logger.error("something went wrong", new Error("boom", { cause: "something" }));
// prints
{
    "level": "ERROR",
    "message": "something went wrong",
    "service": "service_undefined",
    "timestamp": "2023-07-14T11:53:06.959Z",
    "error": {
        "name": "Error",
        "location": "/Users/aamorosi/Codes/error-cause/index.ts:13",
        "message": "boom",
        "stack": "Error: boom\n    at Object.<anonymous> (/Users/aamorosi/Codes/error-cause/index.ts:13:38)\n    at Module._compile (node:internal/modules/cjs/loader:1159:14)\n    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)\n    at Module.load (node:internal/modules/cjs/loader:1037:32)\n    at Module._load (node:internal/modules/cjs/loader:878:12)\n    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)\n    at node:internal/main/run_main_module:23:47",
        "cause": "something"
    }
}
  1. When the error is not present, the error is formatted without any cause key. This is the current behavior and is in line with Logger's behavior of removing undefined values from the logs.
logger.error("something went wrong", new Error("boom"));
// prints
{
    "level": "ERROR",
    "message": "something went wrong",
    "service": "service_undefined",
    "timestamp": "2023-07-14T11:53:06.958Z",
    "error": {
        "name": "Error",
        "location": "/Users/aamorosi/Codes/error-cause/index.ts:11",
        "message": "boom",
        "stack": "Error: boom\n    at Object.<anonymous> (/Users/aamorosi/Codes/error-cause/index.ts:11:38)\n    at Module._compile (node:internal/modules/cjs/loader:1159:14)\n    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)\n    at Module.load (node:internal/modules/cjs/loader:1037:32)\n    at Module._load (node:internal/modules/cjs/loader:878:12)\n    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)\n    at node:internal/main/run_main_module:23:47"
    }
}

@dreamorosi dreamorosi moved this from On hold to Working on it in Powertools for AWS Lambda (TypeScript) Jul 14, 2023
@dreamorosi dreamorosi added confirmed The scope is clear, ready for implementation and removed blocked This item's progress is blocked by external dependency or reason labels Jul 14, 2023
@dreamorosi dreamorosi linked a pull request Jul 16, 2023 that will close this issue
9 tasks
@github-project-automation github-project-automation bot moved this from Working on it to Coming soon in Powertools for AWS Lambda (TypeScript) Jul 17, 2023
@github-actions
Copy link
Contributor

⚠️ COMMENT VISIBILITY WARNING ⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added pending-release This item has been merged and will be released soon and removed confirmed The scope is clear, ready for implementation labels Jul 17, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Jul 25, 2023

This is now released under v1.12.1 version!

@github-actions github-actions bot added completed This item is complete and has been merged/shipped and removed pending-release This item has been merged and will be released soon labels Jul 25, 2023
@dreamorosi dreamorosi moved this from Coming soon to Shipped in Powertools for AWS Lambda (TypeScript) Jul 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
completed This item is complete and has been merged/shipped feature-request This item refers to a feature request for an existing or new utility logger This item relates to the Logger Utility
Projects
Development

Successfully merging a pull request may close this issue.

5 participants