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

websocket errors are not propagated to client #1002

Open
fusionfoxy opened this issue Jan 8, 2025 · 1 comment
Open

websocket errors are not propagated to client #1002

fusionfoxy opened this issue Jan 8, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@fusionfoxy
Copy link

fusionfoxy commented Jan 8, 2025

What version of Elysia is running?

1.2.10

What platform is your computer?

Linux 6.1.0-23-amd64 x86_64 unknown

What steps can reproduce the bug?

import { treaty } from "@elysiajs/eden";
import { describe, expect, test } from "bun:test";
import { Elysia } from "elysia";

const app = new Elysia()
.get("/", () => "Hello Elysia")
.ws('/ws', {
  beforeHandle(ws: any) {
    return ws.error(403, 'forbidden')
  }
})
.listen(4000);

describe("Elysia ws error test", () => {
  test("Elysia ws error", async () => {
    const api = treaty<typeof app>("localhost:4000");

    const ws = api.ws.subscribe();

    const p = new Promise<number>((res, rej) => {
      ws.on('open', () => {
        console.log("OPEN")
        rej();
      });
      ws.on('close', (event) => {
        console.error("CLOSE: ", event.code)
        rej();
      });
      ws.on('error', (event) => {
        console.log("EVENT: ", event)
        res(42);
      })
    })
    return expect(p).resolves.toBe(42);
  });
});

What is the expected behavior?

When the server websocket beforeHandle() returns ws.error(403, 'forbidden') I would expect the client websocket on error to be called with the 403 forbidden error.

What do you see instead?

Instead of the on error callback, the on close callback is called and the websocket status code is 1002 with no trace of the 403 code

$ bun test 
bun test v1.1.29 (6d43b366)

src/app.test.ts:
CLOSE:  1002
10 |   }
11 | })
12 | .listen(4000);
13 | 
14 | describe("Elysia ws error test", () => {
15 |   test("Elysia ws error", async () => {
                               ^
error: 

Expected promise that resolves
Received promise that rejected: Promise { <rejected> }

      at /home/tfk/Documents/elysiaTest/app/src/app.test.ts:15:27
✗ Elysia ws error test > Elysia ws error [4.88ms]

 0 pass
 1 fail
 1 expect() calls
Ran 1 tests across 1 files. [47.00ms]

Additional information

No response

Have you try removing the node_modules and bun.lockb and try again yet?

yes

@fusionfoxy fusionfoxy added the bug Something isn't working label Jan 8, 2025
@hisamafahri
Copy link
Contributor

hisamafahri commented Jan 26, 2025

The problem I believe is in your test.

  • p expected to resolve to 42
  • beforeHandle method is interuppting the websocket handshake, hence it will close the connection (meaning we should listen to the close event in this case)
  • But, your ws.on("close"), instead of resolve, use reject
  • The test case fail

You can fix it by use res instead of rej.

describe("Elysia ws error test", () => {
  test("Elysia ws error", async () => {
    const api = treaty<typeof app>("localhost:4000");

    const ws = api.ws.subscribe();

    const p = new Promise<number>((res, rej) => {
      ws.on("open", () => {
        console.log("OPEN");
        rej();
      });
      ws.on("close", (event) => {
        console.error("CLOSE: ", event.code);
        res(42); //
      });
      ws.on("error", (event) => {
        console.log("EVENT: ", event);
        res(42);
      });
    });

    expect(p).resolves.toBe(42);
  });
});

That test suite should return the following:

bun test v1.2.0 (b0c5a765)

src/index.test.ts:
CLOSE:  1002
✓ Elysia ws error test > Elysia ws error [6.18ms]

 1 pass
 0 fail
 1 expect() calls
Ran 1 tests across 1 files. [16.00ms]

Yes, by that logic, if you expect rejects, and use rej, the test case will pass.

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

2 participants