Skip to content

LDClient.flush() does not reject if there is an error sending analytics #777

Open
@hysmio

Description

@hysmio

Describe the bug
LDClient.flush() does not reject like it states if the underlying EventProcessor.flush() call throws, irrespective of callback.

if a callback is provided, the callback will receive the error, the promise will not reject as intended. If no callback is provided, the promise will still not reject, which is unintended as per the JSDoc.

To reproduce

  1. Call await LDClient.flush() with an explicit eventsUri that has an error status code, like 503.
  2. It will not throw an error
  3. Call it again but with a callback await LDClient.flush((error, success) => console.error(success, error));, you will get a console error output.

Expected behavior
The promise on await LDClient.flush() should reject if there is an error.

Logs
If applicable, add any log output related to your problem.

SDK version

@launchdarkly/[email protected]
@launchdarkly/[email protected]
@launchdarkly/[email protected]

Language version, developer tools

node - v20.11.1

OS/platform

Windows 11
WSL - Ubuntu 24.04

Solution
Added some comments to explain what the solution I think could be, also found that the callback is called with an error and then immediately again without one, so that should also be fixed.

# LDClient.flush
  async flush(callback?: (err: Error | null, res: boolean) => void): Promise<void> {
    try {
      await this._eventProcessor.flush();
    } catch (err) {
      if (!callback) { // If callback isn't set, throw the error to the caller.
        throw err;
      }
      // also this will currently callback twice regardless, there should probably be a return after this call
      callback(err as Error, false);
    }
    callback?.(null, true);
  }
```ts

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpackage: shared/sdk-serverLabel for issues affecting the shared/sdk-server package.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions