postman-app-support icon indicating copy to clipboard operation
postman-app-support copied to clipboard

Global variable (setGlobalVariable) not persisted/saved if the pre-request script throws an error

Open gabipetrovay opened this issue 8 years ago • 4 comments

App Details:

Postman for Windows
Version 5.2.1
win32 6.3.9600 / x64

Issue Report:

  1. I switched to a new environment and the global variables set in the pre-request script are not persisted if the pre-request script throws an error (intentionally, because I want to interrupt the request if some validation is violated)
  2. Expected behaviour: regardless of the way the pre-request script exits, I still want to save global variables (in my case, because I implement a deletion request guard to avoid unintentional DELETE REST calls.)

Example code:

// some logic
postman.setGlobalVariable('_deletion_protection_', guard);
// some more logic
if (...) {
    throw new Error('Are you sure you want to delete this resource? Click ' + (guard + 1) + ' more times to confirm.');
}

After executing this request and when this throws the above error, the _deletion_protection_ variable is not present in the Globals list.

gabipetrovay avatar Oct 06 '17 09:10 gabipetrovay

hi @gabipetrovay,

The assumption now is that only successful request/collection runs will update the environment/global. That being said, it should be possible to do what you are looking for.

Let's say that you had a function to skip the current request from the script without throwing an error, would that solve your use case?

kamalaknn avatar Oct 12 '17 08:10 kamalaknn

There are 3 ellements that are important for our collections to work properly:

  1. Skipping the request if a validation/consistency error is detected
  2. Saving some global variables to perform in-between execution validations and deletion protection
  3. Displaying a human readable UI error to the user showing what he did wrong, what he is missing or what request to execute instead

Until recently, we achieved these 3 only with using throw new Error and postman.setGlobalVariable.

So any alternative/features that help us get the 3 points above would be great.

Notes:

  • we don't control the APIs (so, as someone mentioned I think on SO, we cannot hijack the URLs to send bad requests if we detect errors)
  • it would be nice to have a postman API for notifications (log, warn, err) such that we dont have to abuse the `throw' expressions
  • it would be nice to have a postman.cancelRequest or equivalent in order to stop request execution from pre-request scripts.

gabipetrovay avatar Oct 16 '17 05:10 gabipetrovay

Note: related to solution of #2392

shamasis avatar Sep 18 '18 17:09 shamasis

It seems crazy to me that this happens:

  • Automatically (it's not opt-in)
  • Forcefully (cannot be stopped)
  • Silently (There is no indication that your global variable changes are lost)

Seems like the workaround is to not use globals?

addison-rio avatar Mar 10 '25 07:03 addison-rio