Giter Club home page Giter Club logo

sveltekit-invalid-body's Introduction

SvelteKit Node Adapter Returns an "Invalid request body" HTTP Error that Can Be Misleading and Hard to Debug

A minimal reproducible example.

It simply constists in the skeleton app one gets with npm create svelte@latest (with the "latest" at the time of writing, see package-lock.json), using the Node adapter. To reproduce, simply run the following in your shell:

npm install
npm run build
ORIGIN=localhost:3000 node build # note the invalid value for ORIGIN

Then go to http://localhost:3000/: you should get an "Invalid request body" response with a HTTP 400 Bad Request status.

The error message is misleading because your request obviously does not have an invalid body (it does not even have a body since it is a GET request). Furthermore it is quite hard to debug because the error is not caught by the handleError hook in src/hooks.server.ts.

The cause of the error is that the ORIGIN environment variable is invalid, it should be http://localhost:3000 (or just localhost:3000; see also the MDN page about "origin"). This causes an error in build/handler.js that happens too early to be caught by handleError. The code that handles this error (which does seem like it could be instrumented at the moment) mistakenly assumes that an error here can only be caused by an invalid body, hence the error message: https://github.com/sveltejs/kit/blob/d4043863e5aa6b5fb003ebafef2a6e9098255b45/packages/adapter-node/src/handler.js#L90

	} catch (err) {
		res.statusCode = err.status || 400;
		res.end('Invalid request body');
		return;
	}

If we add a console.error(err); in this catch block we get the following error log:

TypeError: Failed to parse URL from localhost/
    at new Request (file:///home/cedricvr/tinkering/sveltekit-invalid-body/build/shims.js:12760:16)
    at getRequest (file:///home/cedricvr/tinkering/sveltekit-invalid-body/build/handler.js:1031:9)
    ... 4 lines matching cause stack trace ...
    at handle (file:///home/cedricvr/tinkering/sveltekit-invalid-body/build/handler.js:1257:23)
    at file:///home/cedricvr/tinkering/sveltekit-invalid-body/build/handler.js:1257:40
    at Array.<anonymous> (file:///home/cedricvr/tinkering/sveltekit-invalid-body/build/handler.js:653:28)
    at handle (file:///home/cedricvr/tinkering/sveltekit-invalid-body/build/handler.js:1257:23) {
  [cause]: TypeError [ERR_INVALID_URL]: Invalid URL
      at new NodeError (node:internal/errors:387:5)
      at URL.onParseError (node:internal/url:565:9)
      at new URL (node:internal/url:641:5)
      at new Request (file:///home/cedricvr/tinkering/sveltekit-invalid-body/build/shims.js:12758:22)
      at getRequest (file:///home/cedricvr/tinkering/sveltekit-invalid-body/build/handler.js:1031:9)
      at Array.ssr (file:///home/cedricvr/tinkering/sveltekit-invalid-body/build/handler.js:1186:19)
      at handle (file:///home/cedricvr/tinkering/sveltekit-invalid-body/build/handler.js:1257:23)
      at file:///home/cedricvr/tinkering/sveltekit-invalid-body/build/handler.js:1257:40
      at Array.<anonymous> (file:///home/cedricvr/tinkering/sveltekit-invalid-body/build/handler.js:1175:4)
      at handle (file:///home/cedricvr/tinkering/sveltekit-invalid-body/build/handler.js:1257:23) {
    input: 'localhost/',
    code: 'ERR_INVALID_URL'
  }
}

We then see that the error comes from NodeJS builting new URL(...) constructor, due to the invalid origin. We can easily reproduce it on NodeJS itself:

$ docker run -it node:18 node
Welcome to Node.js v18.16.0.
Type ".help" for more information.
> new URL('localhost/');
Uncaught TypeError [ERR_INVALID_URL]: Invalid URL
    at __node_internal_captureLargerStackTrace (node:internal/errors:490:5)
    at new NodeError (node:internal/errors:399:5)
    at new URL (node:internal/url:560:13) {
  input: 'localhost/',
  code: 'ERR_INVALID_URL'
}

sveltekit-invalid-body's People

Contributors

cedricvanrompay avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.