Timo Martinson
Timo Martinson14mo ago

Uncaught (in promise) TypeError: cannot read headers: request closed

What is this? How to fix it?
18 Replies
Sorikairo
Sorikairo14mo ago
You might want to provide some code for people to be able to help you. The error itself is not quite useful, as it is very explicit. Request is closed, so you cannot read headers. You are probably sending a Response, then trying to read headers from the Request.
Timo Martinson
Timo Martinson14mo ago
ahm, ok, sorry.
Sorikairo
Sorikairo14mo ago
The fact that it says "Uncaught (in promise)" hints that you might not be awaiting a promise
Timo Martinson
Timo Martinson14mo ago
as the file is very small, here is it:
import { Application, Router } from 'https://deno.land/x/oak/mod.ts'
import { oakCors } from 'https://deno.land/x/cors/mod.ts'

import { City } from './types/city.ts'
import { Activity } from './types/activity.ts'

// --

const app = new Application()
const router = new Router()

// --

app.use(oakCors())

// --

const kv = await Deno.openKv('./src/database/kv')

// -- Städte --

router.post('/cities', async (context) => {
const id = crypto.randomUUID()
const data = await context.request.body().value
await kv.set(['cities', id], data)
const cityData = await kv.get(['cities', id])
const city: City = { id, ...cityData.value }
context.response.body = { city }
})

router.get('/cities', async (context) => {
const cities: City[] = []
for await (const cityData of kv.list({ prefix: ['cities'] })) {
const id = cityData.key[1]
const city: City = { id, ...cityData.value }
cities.push(city)
}
context.response.body = { cities }
})

// -- Aktivitäten --

router.post('/activities', async (context) => {
const id = crypto.randomUUID()
const data = await context.request.body().value
await kv.set(['activities', id], data)
const activityData = await kv.get(['activities', id])
const activity: Activity = { id, ...activityData.value }
context.response.body = { activity }
})

router.get('/activities', async (context) => {
const activities: Activity[] = []
for await (const activityData of kv.list({ prefix: ['activities'] })) {
const id = activityData.key[1]
const activity: Activity = { id, ...activityData.value }
activities.push(activity)
}
context.response.body = { activities }
})

// --

app.use(router.routes(), router.allowedMethods())

// --

app.listen({ port: 4000 })
import { Application, Router } from 'https://deno.land/x/oak/mod.ts'
import { oakCors } from 'https://deno.land/x/cors/mod.ts'

import { City } from './types/city.ts'
import { Activity } from './types/activity.ts'

// --

const app = new Application()
const router = new Router()

// --

app.use(oakCors())

// --

const kv = await Deno.openKv('./src/database/kv')

// -- Städte --

router.post('/cities', async (context) => {
const id = crypto.randomUUID()
const data = await context.request.body().value
await kv.set(['cities', id], data)
const cityData = await kv.get(['cities', id])
const city: City = { id, ...cityData.value }
context.response.body = { city }
})

router.get('/cities', async (context) => {
const cities: City[] = []
for await (const cityData of kv.list({ prefix: ['cities'] })) {
const id = cityData.key[1]
const city: City = { id, ...cityData.value }
cities.push(city)
}
context.response.body = { cities }
})

// -- Aktivitäten --

router.post('/activities', async (context) => {
const id = crypto.randomUUID()
const data = await context.request.body().value
await kv.set(['activities', id], data)
const activityData = await kv.get(['activities', id])
const activity: Activity = { id, ...activityData.value }
context.response.body = { activity }
})

router.get('/activities', async (context) => {
const activities: Activity[] = []
for await (const activityData of kv.list({ prefix: ['activities'] })) {
const id = activityData.key[1]
const activity: Activity = { id, ...activityData.value }
activities.push(activity)
}
context.response.body = { activities }
})

// --

app.use(router.routes(), router.allowedMethods())

// --

app.listen({ port: 4000 })
Sorikairo
Sorikairo14mo ago
which line throws the error ?
Timo Martinson
Timo Martinson14mo ago
when the error occurs, both files (kv-shm and kv-wal) are deleted. i don't know the exact line it occurs while editing data on the client (react) seems it has something to do with KV !??!?! or deno run --watch ... as script ? i am guessing.
Sorikairo
Sorikairo14mo ago
So it fails when you do a POST request to one of the endpoint ? Error should have a stacktrace so you can deduct where the error come from, do not hesitate to post it here
Timo Martinson
Timo Martinson14mo ago
i will, just waiting for it to occur again
Sorikairo
Sorikairo14mo ago
Oh, is it not happening every time ? Makes it quite hard 😭
Timo Martinson
Timo Martinson14mo ago
yes it is. it is rarely happening. i think it might be a bug from early KV as i said. both database files are missing after the error
error: Uncaught (in promise) TypeError: cannot read headers: request closed
return this.#request.headers;
^
at Object.get headerList (ext:deno_fetch/23_request.js:115:17)
at Request.request.<computed> (ext:deno_fetch/23_request.js:565:60)
at Request.get [headers] (ext:deno_fetch/23_request.js:252:46)
at Request.get headers (ext:deno_fetch/23_request.js:452:16)
at NativeRequest.get headers (https://deno.land/x/oak@v11.1.0/http_server_native_request.ts:68:26)
at new Request (https://deno.land/x/oak@v11.1.0/request.ts:145:21)
at new Context (https://deno.land/x/oak@v11.1.0/context.ts:173:20)
at Application.#handleRequest (https://deno.land/x/oak@v11.1.0/application.ts:425:21)
at Application.listen (https://deno.land/x/oak@v11.1.0/application.ts:610:28)
at eventLoopTick (ext:core/01_core.js:182:11)
error: Uncaught (in promise) TypeError: cannot read headers: request closed
return this.#request.headers;
^
at Object.get headerList (ext:deno_fetch/23_request.js:115:17)
at Request.request.<computed> (ext:deno_fetch/23_request.js:565:60)
at Request.get [headers] (ext:deno_fetch/23_request.js:252:46)
at Request.get headers (ext:deno_fetch/23_request.js:452:16)
at NativeRequest.get headers (https://deno.land/x/oak@v11.1.0/http_server_native_request.ts:68:26)
at new Request (https://deno.land/x/oak@v11.1.0/request.ts:145:21)
at new Context (https://deno.land/x/oak@v11.1.0/context.ts:173:20)
at Application.#handleRequest (https://deno.land/x/oak@v11.1.0/application.ts:425:21)
at Application.listen (https://deno.land/x/oak@v11.1.0/application.ts:610:28)
at eventLoopTick (ext:core/01_core.js:182:11)
this is the full log maybe the problem is at the side of the react-client which breaks the connection ???
Sorikairo
Sorikairo14mo ago
Looks like an issue with oak, where the connection dies unexpectedly
Timo Martinson
Timo Martinson14mo ago
ok
Sorikairo
Sorikairo14mo ago
Does the react client crashes or something ? Because requests ending unexpectedly when working locally seems pretty hard to do
Timo Martinson
Timo Martinson14mo ago
yes.
Timo Martinson
Timo Martinson14mo ago
Unhandled Rejection (TypeError): Failed to fetch
Unhandled Rejection (TypeError): Failed to fetch
Timo Martinson
Timo Martinson14mo ago
I am working with parcel as a bundler. i am switching to vite. let's see if that works there seems to be a solution
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Timo Martinson
Timo Martinson14mo ago
no, I did not find a solution.