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

Stream can't handle exceptions #982

Open
macabeus opened this issue Dec 27, 2024 · 0 comments
Open

Stream can't handle exceptions #982

macabeus opened this issue Dec 27, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@macabeus
Copy link
Contributor

macabeus commented Dec 27, 2024

What version of Elysia is running?

^1.2.7

What platform is your computer?

Darwin 24.1.0 arm64 arm

What steps can reproduce the bug?

Run this test, written based on test/response/stream.test.ts:

it('handle stream with exception', async () => {
  const expected = ['a', 'handled']

  const app = new Elysia().get(
    '/',
    async function* () {
      yield 'a'
      await Bun.sleep(10)
      throw new Error('My Dummy Error')
    },
    {
      error({ error }) {
        return 'handled'
      }
    }
  )

  const response = await app
    .handle(req('/'))
    .then((x) => x.body)
    .then((x) => {
      if (!x) return

      const reader = x?.getReader()

      let acc = ''
      const { promise, resolve } = Promise.withResolvers()

      reader.read().then(function pump({ done, value }): unknown {
        if (done) return resolve(acc)

        expect(value.toString()).toBe(expected.shift()!)

        acc += value.toString()
        return reader.read().then(pump)
      })

      return promise
    })

  expect(expected).toHaveLength(0)
  expect(response).toBe('ahandled')
})

What is the expected behavior?

I expected that error would handle the exception.

What do you see instead?

The error handler is never called, and the connection isn't closed properly.
On Postman, I see this error:

name: "Error"
code: "HPE_INVALID_CHUNK_SIZE"
message: "Parse Error: Invalid character in chunk size"

and then the connection is closed.

@macabeus macabeus added the bug Something isn't working label Dec 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant