Skip to content

"TypeError: fetch failed" is an incomplete error: fetch in Server Component, if throws and isn't caught, loses info #49896

@tomsseisums

Description

@tomsseisums

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: linux
      Arch: x64
      Version: #22 SMP Tue Jan 10 18:39:00 UTC 2023
    Binaries:
      Node: 16.17.0
      npm: 8.15.0
      Yarn: 1.22.19
      pnpm: 7.1.0
    Relevant packages:
      next: 13.4.3-canary.1
      eslint-config-next: N/A
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 4.9.5

Which area(s) of Next.js are affected? (leave empty if unsure)

App directory (appDir: true)

Link to the code that reproduces this issue

https://codesandbox.io/p/sandbox/next-js-server-components-fetch-incomplete-error-if-uncaught-o09yqm

To Reproduce

In server component, try to load data from an non-existent URL without try/catch.

await fetch('http://127.0.0.1:1337/non-existent')

Describe the Bug

The terminal logs just:

TypeError: fetch failed

And with next dev, it will be surfaced to frontend with just the same:

Unhandled runtime error
TypeError: fetch failed

If I include my own error.tsx and try to destructure the error into const { name, message, trace, cause, ...rest } = error, it will still be incomplete, having only name and message (and digest in ...rest), and there the name even will be dropped from TypeError to just Error.

Related: #44062, but there the focus isn't on error being incomplete, rather that it happens at all.

The issue seems that it only surfaces first line for low-level errors (ECONNREFUSED etc.). Which is why the #44062 seems like a mysterious thing, albeit if you look inside, you'd see that it's the localhost resolving to ::1 IPv6 issue at most cases.

Expected Behavior

Given I try/catch and console.error('FULL ERROR', e) the error and get this out:

FULL ERROR:  TypeError: fetch failed
    at Object.fetch (/project/sandbox/node_modules/next/dist/compiled/undici/index.js:1:26669)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  cause: Error: connect ECONNREFUSED 127.0.0.1:1337
      at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1247:16)
      at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
    errno: -111,
    code: 'ECONNREFUSED',
    syscall: 'connect',
    address: '127.0.0.1',
    port: 1337
  }
}
  1. I would expect this to be shown by default in terminal logs. Without me having to try/catch + console.error to see the details.
  2. I would expect the error.tsx to also have the full info available, as it is for any other (although with this I'm not sure) uncaught exception in Server Component.
  3. I would expect the Error surfaced to error.tsx not to lose it's name (error.tsx shows Error instead of TypeError).

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIssue was opened via the bug report template.locked

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions