Skip to content
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

feat(logger): add cause field to formatted error #1617

Merged
merged 1 commit into from
Jul 17, 2023

Conversation

dreamorosi
Copy link
Contributor

Description of your changes

This PR introduces new logic to the LogFormatter class to allow the Logger utility to include the cause field to logs, when emitting a log that contains an Error. The cause field was added in Node.js 16 so we need to use a conditional logic to check if the Error passed in to the logger has the field or not before attempting to format it and include it in a log entry.

This logic is meant to be temporary and will be removed once we drop support for Node.js 14. This is documented in the docstrings of the new code.

Once this PR is merged, customers can pass an Error with a cause to the logger, which will format it like so:

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"
        }
    }
}

Note that in JS it's possible to throw more objects than just Error, for this reason the logic also includes a check instanceof Error before attempting to format the cause. If the object is not an error it will be included as value of the cause key 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"
    }
}

Related issues, RFCs

Issue number: #1361

Checklist

  • My changes meet the tenets criteria
  • I have performed a self-review of my own code
  • I have commented my code where necessary, particularly in areas that should be flagged with a TODO, or hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my change is effective and works
  • The PR title follows the conventional commit semantics

Breaking change checklist

Is it a breaking change?: NO

  • I have documented the migration process
  • I have added, implemented necessary warnings (if it can live side by side)

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@dreamorosi dreamorosi requested a review from a team as a code owner July 16, 2023 21:45
@dreamorosi dreamorosi self-assigned this Jul 16, 2023
@dreamorosi dreamorosi linked an issue Jul 16, 2023 that may be closed by this pull request
2 tasks
@pull-request-size pull-request-size bot added the size/M PR between 30-99 LOC label Jul 16, 2023
@boring-cyborg boring-cyborg bot added logger This item relates to the Logger Utility tests PRs that add or change tests labels Jul 16, 2023
@github-actions github-actions bot added the feature PRs that introduce new features or minor changes label Jul 16, 2023
@am29d am29d merged commit 6a14595 into main Jul 17, 2023
10 checks passed
@am29d am29d deleted the 1361-feature-request-include-cause-in-stack-trace branch July 17, 2023 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature PRs that introduce new features or minor changes logger This item relates to the Logger Utility size/M PR between 30-99 LOC tests PRs that add or change tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Include cause in stack trace
2 participants