usebruno / bruno

Opensource IDE For Exploring and Testing Api's (lightweight alternative to postman/insomnia)
https://www.usebruno.com/
MIT License
28.01k stars 1.3k forks source link

Exception when script references an element in an empty response array #127

Open dcoomber opened 1 year ago

dcoomber commented 1 year ago

The request includes a script that retrieves the first element in the response array for use as a collection variable in later requests.

The following toast exception is displayed:

Error invoking remote method 'send-http-request': TypeError: Cannot read properties of undefined (reading 'bookingid')

which relates to the request script:

let data = res.getBody();
bru.setVar("bookingId", data[0].bookingid);

Steps to reproduce

Using this collection: https://github.com/dcoomber/exploring-bruno/tree/dpc_session_1/exploration/02-create/EE%20Bookings

  1. Visit test site http://hotel-test.equalexperts.io/ in your browser and delete all entries
  2. In Bruno, load the collection from my GitHub link above
  3. Select the "Production" environment
  4. Run the "Retrieve list of bookings" request

Toast exception is displayed

Expected result

The "failing" script statement is ignored. No toast exception is displayed. The collection variable is left in its previous state / value.

Please also test the fix in the case where referencing an element out-of-bounds in a non-empty array response.

helloanoop commented 1 year ago

@dcoomber We should throw an error when a script is trying to access an element inside the response which is not present right ? If we don't catch these errors, then the further steps around retrieve the booking would fail.

I may be missing your intent/reasoning here. Can you elaborate on why you think that the script should not throw error in case of an out-of-bounds or invalid-index access ?

dcoomber commented 1 year ago

I'm looking at this from the perspective of winning over users by making sure that the behaviour is somewhat similar to their experience in Postman. Postman does not show an error to the user for the same condition.

That said, showing the exception to the user is less of an issue than the content of the toast message - not all of our users will be technical.

It took me a moment to figure out that the issue was not in the HTTP request, but rather in processing of the response in the post response script. I think that the user experience for less technical users could be improved by either not showing the exception or by using user-friendly text.

helloanoop commented 1 week ago

I am in agreement that we should not throw the error in the response pane, but have it displayed separately. This the current behaviour today on v1.25.0 as on 23 Nov 2025

This is definitely not a good UX. @lohxt1 @anusree-bruno lets have this addressed in the new debug console that we are building.

image