EGamalielGZ
EGamalielGZ10mo ago

Error only happens when I'm on deno deploy

Please can someone explain why with my web application when deployed in deno deploy I get this response and error when trying to login but not when I login locally? Response: Response { body: ReadableStream { locked: false }, bodyUsed: false, headers: Headers { "content-length": "98", "content-type": "text/plain; charset=utf-8", date: "Fri, 06 Oct 2023 03:51:17 GMT", server: "deno/gcp-us-south1", via: "http/1.1 edgeproxy" }, ok: false, redirected: false, status: 508, statusText: "Loop Detected", url: "https://roomhome.deno.dev/api/auth/client/login" } Error: An error occurred during route handling or page rendering. SyntaxError: Unexpected non-whitespace character after JSON at position 3 (line 1 column 4) at parse (<anonymous>) at packageData (ext:deno_fetch/22_body.js:369:14) at consumeBody (ext:deno_fetch/22_body.js:246:12) at eventLoopTick (ext:core/01_core.js:183:11) at async POST (file:///src/routes/login.tsx:40:32) at async handler (file:///src/routes/_middleware.ts:32:14) at async handler (https://deno.land/x/fresh@1.4. Repo: https://github.com/EGAMAGZ/RoomHome
GitHub
GitHub - EGAMAGZ/RoomHome: Tienda de inmobilaria
Tienda de inmobilaria. Contribute to EGAMAGZ/RoomHome development by creating an account on GitHub.
10 Replies
Deno AI Helper
Deno AI Helper10mo ago
Based on the error message you provided, it seems like the issue is related to parsing JSON data. The error message Unexpected non-whitespace character after JSON at position 3 (line 1 column 4) typically indicates that the JSON data you're trying to parse is not formatted correctly. The error occurs at file:///src/routes/login.tsx:40:32, which suggests that the issue is in your login.tsx file at line 40, column 32. Unfortunately, without access to your code, it's hard to provide a more specific solution. However, the documentation provides some general advice for handling errors in Deno: 1. Check the JSON data: Make sure the JSON data you're trying to parse is correctly formatted. You can use online JSON validators to check this. 2. Handle potential errors: The req.text() call can fail if the user hangs up the connection before the body is fully received. Make sure to handle this case. This can happen in all methods that read from the request body, such as req.json(), req.formData(), req.arrayBuffer(), req.body.getReader().read(), req.body.pipeTo(), etc. 3. Check your routes: The documentation provides examples of how to handle routes and form submissions. Make sure your routes are set up correctly and that you're handling form submissions properly. 4. Debugging: If you're still having issues, try to isolate the problem. Can you log the JSON data before you parse it? Does the error still occur if you hard-code some JSON data instead of getting it from the request? Remember, these are general suggestions and may not directly solve your issue. If you're still having trouble, you might want to ask for help in the Deno community or on Stack Overflow. 🦕
ioB
ioB10mo ago
You're not allowed to send a request to youself in Deno Deploy. Call the function directly!
EGamalielGZ
EGamalielGZ10mo ago
Why I'm not allowed when the platform gives you the possibility to deploy REST APIs, and also I can do HTTP Request locally with problem from the proyect running locally on requesting with Postman? I don't get it 😵‍💫
cknight
cknight10mo ago
It's at least partly for protection of the Deploy infra to avoid applications getting into infinite loops where they call themselves, which call themselves, which call themselves, which...
ioB
ioB10mo ago
It's mostly ^ but there's also a performance thing, why go through the network! You're on the server regardless!
EGamalielGZ
EGamalielGZ10mo ago
So, in the case I want to fetch my own API how can I do it?
ioB
ioB10mo ago
Just... run the code the api runs?
// /api/whatever.ts
export function getResponse() {
return new Response("hello world");
}

function handler(req: Request) {
return getResponse();
}

// /api/another.ts
import { getResponse } from "./whatever.ts";

function handler(req: Request) {
const res = getResponse();
// some more logic
}
// /api/whatever.ts
export function getResponse() {
return new Response("hello world");
}

function handler(req: Request) {
return getResponse();
}

// /api/another.ts
import { getResponse } from "./whatever.ts";

function handler(req: Request) {
const res = getResponse();
// some more logic
}
EGamalielGZ
EGamalielGZ10mo ago
Ok, so I am supossed to divide the logic from the api from the Handler so then I'll be able to consume it? Kida weird, but it might work, thanks. If i have doubts I'll ask
ioB
ioB10mo ago
yup 👍
EGamalielGZ
EGamalielGZ10mo ago
Also,I forgot to include somethin' on what i said. This error only happens when I do POST query