Skip to content

Allow custom HTTPException for node-ws upgrade #1573

@krzysztofciombor-solidstudio

Description

Which middleware is the feature for?

@hono/node-ws

What is the feature you are proposing?

There are multiple cases where a custom HttpException thrown in upgradeWebSocket method would make sense, in particular throwing an HTTP 401 for Unauthorized requests.
Currently, when using @hono/node-ws, no matter which HttpException is thrown, the Websocket will be closed with HTTP 400 (as seen here: https://github.com/honojs/middleware/blob/main/packages/node-ws/src/index.ts#L85)

For example:

app.get(
  "/ws",
  upgradeWebSocket(async (c) => {
    if (!isAuthorized(c.user)) {
      throw new HTTPException(401, { message: "Unauthorized" });
    }
    ...
  }
)

The expected result is to receive HTTP/1.1 401 Unauthorized response, but the actual result is: HTTP/1.1 400 Bad Request

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions