Skip to content

onMessage calls the callback twice, with the raw string, when the callback throws #502

Open

Description

@RaphaelFakhri

ReactNativeBrownfield.onMessage in packages/react-native-brownfield/src/index.ts calls the callback inside the same try block as JSON.parse:

try {
  callback({ data: JSON.parse(payload.text) });
} catch {
  callback({ data: payload.text });
}

If the callback throws while it handles a valid JSON message, the catch branch runs the callback again with the unparsed string as data, and the original error is discarded.

Steps to reproduce

  1. Subscribe with a callback that throws, for example ReactNativeBrownfield.onMessage(() => { throw new Error('boom'); }).
  2. From the native host, post a JSON message such as {"type":"login"}.

Expected behavior

The callback runs once with data set to { type: 'login' }, and the boom error surfaces.

Actual behavior

The callback runs twice: first with { type: 'login' }, then with the string '{"type":"login"}'. The boom error from the first call is swallowed. Handlers that are not idempotent run their side effects twice, and the second call usually fails with a different, misleading error.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions