jsonata-js / jsonata

JSONata query and transformation language - http://jsonata.org
MIT License
2.05k stars 220 forks source link

Error handling and asynchronous behavior in evaluate function #670

Open jacobgoh101 opened 8 months ago

jacobgoh101 commented 8 months ago
  1. Problem description: In the JSONata library, the evaluate function does not properly handle errors when a callback function is used. The error is thrown without being returned to the callback function, causing unhandled errors. It is expected that the error should be returned in the callback function. The problematic code can be found here:

    https://github.com/jsonata-js/jsonata/blob/d7790e873194bfebc3719ab08983b5a58fa2848b/src/jsonata.js#L2094-L2141

  2. Inconsistent behavior: The documentation and this comment suggests using a callback function to enable asynchronous mode. However, during testing, it seems that the asynchronous function gets evaluated regardless of whether a callback function is used or not.

    image image

Reproduction Steps:

  1. Use the evaluate function with a callback function to handle the result asynchronously.
  2. Trigger an error condition that should result in an error being returned.
  3. Observe that the error is not returned through the callback function, but instead thrown directly.

Expected Behavior:

When a callback function is used, the evaluate function should return the error through the callback function instead of throwing it directly. Additionally, the asynchronous behavior should only take effect when a valid callback function is provided.

Environment: