larksuite / larksuite/oapi-sdk-java

EventDispatcher: 未注册的事件类型抛出 HandlerNotFoundException 导致 WebSocket 断连

Open
#185 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
310
Forks
115
PR merge metrics
No merged PRs in 30d

Description

问题描述

当 WebSocket 长连接收到未在 EventDispatcher 中注册 handler 的事件类型时,SDK 抛出 HandlerNotFoundException。该异常在 ws.Client.handleDataFrame 中被内部 catch,SDK 发送 500 响应给飞书服务端,可能导致服务端主动断开 WebSocket 连接。

关键问题:异常在 SDK 内部被吞掉,应用层无法感知,且 SDK 没有提供 fallback handler 机制来让外部代码处理未注册的事件。

复现路径

  1. 使用 WebSocket 长连接模式
  2. EventDispatcher 只注册部分 IM 事件的 handler(如 P2MessageReceiveV1
  3. 飞书服务端推送未注册的事件(如 im.message.read_v1im.chat.member.bot.deleted_v1 等)
  4. SDK 抛出 HandlerNotFoundException → catch → 发送 500 → 连接可能断开

期望行为

  1. EventDispatcher.Builder 提供 fallback handler 机制(如 .onUnhandledEvent(handler)),让外部代码可以统一处理未注册的事件类型
  2. 或者未注册的事件类型被静默忽略(返回 200),而非抛异常导致 500 响应

当前版本

oapi-sdk: 2.6.1

当前 workaround

EventDispatcher.Builder 上为所有可能的事件类型注册空 handler,但这需要手动维护,且无法覆盖 SDK 未来新增的事件类型。

// 需要手动注册所有已知事件的空 handler
EventDispatcher.newBuilder("", "")
    .onP2MessageReceiveV1(handler)
    .onP2MessageReactionCreatedV1(event -> {})
    .onP2MessageReactionDeletedV1(event -> {})
    .onP2MessageReadV1(event -> {})
    .onP2MessageRecalledV1(event -> {})
    .onP2ChatMemberBotAddedV1(event -> {})
    .onP2ChatMemberBotDeletedV1(event -> {})
    // ... 还有更多
    .build();

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing EventDispatcher.Builder and ws.Client.handleDataFrame, then inspect how HandlerNotFoundException is caught and converted into a 500 response. Clarify whether the chosen behavior is a fallback handler or silent success for unregistered events. Done means unregistered WebSocket events are handled without causing the connection to be closed.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, backend, networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.