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

serve() error: "Uncaught InvalidData" #7891

Closed
nzakas opened this issue Oct 9, 2020 · 4 comments
Closed

serve() error: "Uncaught InvalidData" #7891

nzakas opened this issue Oct 9, 2020 · 4 comments
Labels

Comments

@nzakas
Copy link

nzakas commented Oct 9, 2020

I've been trying to run Deno in Google Cloud Run and while I could get my code to run locally using Docker, I kept getting the same error when trying to run in Cloud Run.

Code:

import { serve } from "https://deno.land/std@0.74.0/http/server.ts";

const PORT = Deno.env.get("PORT") || 8080;
const HOST = "0.0.0.0";

const server = serve({ hostname: HOST, port: PORT });
console.log(`HTTP webserver running.  Access it at:  http://${ HOST }:${ PORT }/`);

for await (const request of server) {
    request.respond({ status: 200, body: "Hello world!" });
}

The errors I received in Cloud Run are (may be a bit messed up, I tried unescaping the logs):

error: Uncaught InvalidData: data did not match any variant of untagged enum ArgsEnum
at Object.jsonOpSync (core.js:247:13)
at opListen ($deno$/cli/rt/30_net.js:32:17)
at Object.listen ($deno$/cli/rt/30_net.js:207:17)
at serve (server.ts:287:25)
at file:///app/src/index.js:22:16

My Dockerfile is:

FROM hayd/alpine-deno

WORKDIR /app
USER deno

EXPOSE $PORT

COPY . .
RUN deno cache src/index.js

CMD ["run", "--allow-net", "--allow-env", "src/index.js"]

This Dockerfile worked fine locally but I got an error when using in CloudRun. I was able to resolve the error by changing the argument to serve() to a string, like this:

// results in error
const server = serve({ hostname: HOST, port: PORT });

// results in error
const server = serve({ port: PORT });

// works
const server = serve(`${ HOST }:${ PORT }`);

I'm not sure quite what's happening, but it appears the validation is failing when passing in an object as the argument with the same data as the string.

From digging around in the source, it seems like this may be where the error is coming from.

Theory: The biggest difference between Cloud Run and my local setup is that Cloud Run pulls the value of PORT from an environment variable, which makes it a string, whereas my fallback value is a number (used locally). So, my guess is that somewhere in Deno, there is code expecting a number for the port, and when that check fails, this error occurs. I don't have the time to look at this further tonight but I can check tomorrow.

@lucacasonato
Copy link
Member

which makes it a string, whereas my fallback value is a number (used locally). So, my guess is that somewhere in Deno, there is code expecting a number for the port, and when that check fails, this error occurs

This is exactly what I am thinking too. This could definitely use a more helpful error message.

@nzakas
Copy link
Author

nzakas commented Oct 9, 2020

Just verified: indeed it is the case that if PORT is a string, I get this error. 👍 to having a better error message, though it seems like this could be addressed by converting to a number if the port value is not a number (Express does this, afaik).

@nzakas
Copy link
Author

nzakas commented Oct 27, 2020

If anyone could point me in the right direction, I'd be happy to investigate and see if I can come up with a solution. Where could I start looking for where this error might be occurring?

@stale
Copy link

stale bot commented Jan 6, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jan 6, 2021
@stale stale bot closed this as completed Jan 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants