Using Sveltekit: fatal crash if api is not responding #276

Closed
opened 2026-03-13 07:40:28 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @bfovez on GitHub (Nov 24, 2024).

Describe the bug
Using Sveltekit.
If the API (set with baseUrl option if createAuthClient) is not responding, a call the client.useSession() causes a fatal crash instead of reporting an error:

node:internal/deps/undici/undici:12345
    Error.captureStackTrace(err, this);
          ^

TypeError: fetch failed
    at node:internal/deps/undici/undici:12345:11
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async betterFetch (file:///C:/________/node_modules/@better-fetch/fetch/dist/index.js:527:18)
    at async $fetch (file:///C:/________/node_modules/@better-fetch/fetch/dist/index.js:236:12) {
  cause: AggregateError
      at internalConnectMultiple (node:net:1114:18)
      at afterConnectMultiple (node:net:1667:5)
      at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
    code: 'ECONNREFUSED',
    [errors]: [
      Error: connect ECONNREFUSED ::1:3001
          at createConnectionError (node:net:1634:14)
          at afterConnectMultiple (node:net:1664:40)
          at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
        errno: -4078,
        code: 'ECONNREFUSED',
        syscall: 'connect',
        address: '::1',
        port: 3001
      },
      Error: connect ECONNREFUSED 127.0.0.1:3001
          at createConnectionError (node:net:1634:14)
          at afterConnectMultiple (node:net:1664:40)
          at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
        errno: -4078,
        code: 'ECONNREFUSED',
        syscall: 'connect',
        address: '127.0.0.1',
        port: 3001
      }
    ]
  }
}

To Reproduce
Steps to reproduce the behavior:

  1. Get the Better-Auth Sveltekit example

  2. To simulate an non responding API, in the src/lib/authClient.ts file, change this:

export const client = createAuthClient({
	baseURL: "http://localhost:3000",
});

into this:

export const client = createAuthClient({
	baseURL: "http://localhost:3001", // <--- from 3000 to 3001
});
  1. Launch the server with npm run dev

  2. Visit http://localhost:3000/ and see the error in the console.

Expected behavior
No error.

Desktop (please complete the following information):

  • OS: Windows 11
  • Browser: Chrome
  • Version: Node 21
Originally created by @bfovez on GitHub (Nov 24, 2024). **Describe the bug** Using Sveltekit. If the API (set with `baseUrl` option if `createAuthClient`) is not responding, a call the `client.useSession()` causes a fatal crash instead of reporting an error: ``` node:internal/deps/undici/undici:12345 Error.captureStackTrace(err, this); ^ TypeError: fetch failed at node:internal/deps/undici/undici:12345:11 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async betterFetch (file:///C:/________/node_modules/@better-fetch/fetch/dist/index.js:527:18) at async $fetch (file:///C:/________/node_modules/@better-fetch/fetch/dist/index.js:236:12) { cause: AggregateError at internalConnectMultiple (node:net:1114:18) at afterConnectMultiple (node:net:1667:5) at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) { code: 'ECONNREFUSED', [errors]: [ Error: connect ECONNREFUSED ::1:3001 at createConnectionError (node:net:1634:14) at afterConnectMultiple (node:net:1664:40) at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) { errno: -4078, code: 'ECONNREFUSED', syscall: 'connect', address: '::1', port: 3001 }, Error: connect ECONNREFUSED 127.0.0.1:3001 at createConnectionError (node:net:1634:14) at afterConnectMultiple (node:net:1664:40) at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) { errno: -4078, code: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 3001 } ] } } ``` **To Reproduce** Steps to reproduce the behavior: 1. Get the [Better-Auth Sveltekit example](https://github.com/better-auth/better-auth/tree/main/examples/svelte-kit-example) 2. To simulate an non responding API, in the `src/lib/authClient.ts` file, change this: ```ts export const client = createAuthClient({ baseURL: "http://localhost:3000", }); ``` into this: ```ts export const client = createAuthClient({ baseURL: "http://localhost:3001", // <--- from 3000 to 3001 }); ``` 4. Launch the server with `npm run dev` 5. Visit `http://localhost:3000/` and see the error in the console. **Expected behavior** No error. **Desktop (please complete the following information):** - OS: Windows 11 - Browser: Chrome - Version: Node 21
Author
Owner

@Bekacru commented on GitHub (Nov 24, 2024):

That's expected behavior. It's thrown by fetch.

@Bekacru commented on GitHub (Nov 24, 2024): That's expected behavior. It's thrown by `fetch`.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#276