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

fix: Error w/ cause stack #50

Open
wants to merge 3 commits into
base: 2.x
Choose a base branch
from

Conversation

coltonehrman
Copy link

@coltonehrman coltonehrman commented Aug 11, 2024

To replace: #49

The cause stack in the Error class was not included in the response text.

This uses the native util.format() API to log out the Error stack, which includes the cause. This is essentially the same as console.[error|log]() for an Error.

This PR closes expressjs/express#5630
This PR closes #41

@coltonehrman coltonehrman changed the title feat: Error w/ cause stack feat: Error w/ cause stack Aug 11, 2024
@coltonehrman coltonehrman marked this pull request as ready for review August 11, 2024 00:50
@coltonehrman coltonehrman changed the title feat: Error w/ cause stack fix: Error w/ cause stack Aug 11, 2024
@coltonehrman
Copy link
Author

It seems like an existing test case was already failing on master.

1 failing

  1) finalhandler(req, res)
       404 response
         should encode bad pathname characters:

      AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:

400 !== 404

      + expected - actual

      -400
      +404
      
      at IncomingMessage.onend (test/support/utils.js:79:20)
      at IncomingMessage.emit (node:events:532:35)
      at endReadableNT (node:internal/streams/readable:1696:12)
      at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

test/test.js Outdated
it('should return Error message with 1 level cause trace', function (done) {
var err = new Error('foo', { cause: new Error('bar') })

var expectedError = `Error: foo
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think we want tests which couple to the entire stack trace of mocha and this library. Similarly, we dont want the test to couple to the full html. Both of these mean even small unrelated changes can make this one brittle.

test/test.js Outdated
// The pathRegex is meant to find/replace the `/Users/.../finalhandler` parts of the expectedError
// The pathRegex is also intended to work when ran on other computers and from different root paths.
// Warning: this may or may not work for your local environment
var pathRegex = /\/Users(?:\/[^/]+){3}/g;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As with the above comment, this is much too brittle. I would prefer we just use the most simple string contains style check here to ensure the cause error is included. If you like though, you can also see above some tests which dont have so much hard coded assumptions in them.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, what is the best way to go about testing this? I would prefer something a little more explicit in terms of checking the exact output vs just doing a string match against parts of the output.

My reasoning against the string match is that is leaves a lot of assumptions and doesn't necessarily validate that we get the full error output that we want in this case.

I actually had another possibly way to "mask" user/env specific paths via the process.cwd() and find/replace it in the final output. What do you think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if this is better? 39909fb

* fix: ignore status message for HTTP/2

* test: fix

* test: fix node@9

* refactor: tests

* test: fix

* test: fix syntax
@wesleytodd wesleytodd changed the base branch from master to 2.x August 31, 2024 18:41
wesleytodd and others added 2 commits August 31, 2024 13:41
This is a replacement for pillarjs#49.
I just pulled out the specific change related to this issue and added it here.

Closes: expressjs/express#5630

test: Error w/ cause case

fix: recursively find Error.cause stacks

Remove the error.stack logic as it is redundant

test: check for both 1 level & 2 level Error.cause

refactor: use native util.format() API for Error printing

fix: put back original lines of code

test: update tests to be less brittle
@wesleytodd
Copy link
Member

Error: Parse Error: Expected HTTP/

I have been seeing this error with supertest in a bunch of places. This is the first time it is consistent and happening in CI though. I dont think this is becaues of your changes, but I am going to push a change to make these run in serial which is the way I have fixed this in other libs. I assume superagent is broken, but I don't have time to debug that.

@wesleytodd
Copy link
Member

wesleytodd commented Aug 31, 2024

Oh, weird, it is more than just that. This is breaking because of something in this pr. I think it is the underlying error I mentioned, but then causing tests to fail in a bad way because of it.

I think we are going to need to fix these. If you get a chance to look at them let me know. If not, then I might have to publish 2.0 before we can merge this. Ideally though we can land this as a minor.

@coltonehrman
Copy link
Author

Oh, weird, it is more than just that. This is breaking because of something in this pr. I think it is the underlying error I mentioned, but then causing tests to fail in a bad way because of it.

I think we are going to need to fix these. If you get a chance to look at them let me know. If not, then I might have to publish 2.0 before we can merge this. Ideally though we can land this as a minor.

I'm fine with either way. I can wait for 2.0.

I'm not sure what the issues might be related to this PR. It is basically a 1 line change (not accounting for the changes you added).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Print causes when outputting error stacks Error cause is not displayed
3 participants