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

fix: propagate batchFn sync throws to the loader instead of crashing #318

Merged
merged 2 commits into from
Jan 13, 2023

Conversation

boopathi
Copy link
Contributor

If the batch load function throws synchronously, the error is not caught anywhere in the load pipeline. A synchronous error in the batchLoadFn cannot be caught. It crashes the server as an uncaught exception. Since the batchLoadFn is invoked as a result of .load the errors happening inside should also be available at the .load call.

This PR fixes this issue by wrapping a try catch around the batchLoadFn call and returning a failed dispatch.

const loader = new Dataloader( keys => {
  const mergedKeys = functionThatCanThrowSynchronously(keys);
  // correctly returns a Promise as expected by the Dataloader
  return fetch(mergedKeys).then(mergedValues => split(mergedValues));
});

For example, consider the following minimal repro -

const loader = new DataLoader(() => {
  throw new Error("mock error");
});

try {
  loader.load(1).catch(e => {
    // mock error will not be caught here
  })
} catch (e) {
  // mock error will also not be caught here
}

With this PR, the "mock error" would be caught in the load.catch handler.

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Oct 21, 2022

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: boopathi / name: Boopathi Rajaa (6381616)

@boopathi boopathi force-pushed the fix-batch-load-sync-throw branch from 6381616 to 5ea66e8 Compare January 9, 2023 13:01
@oporkka
Copy link
Contributor

oporkka commented Jan 10, 2023

We had a past experience that this bug caused our application crash in a hard to discover way because of the uncaught exception it allows. What would be needed to get this merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants