postman-app-support
postman-app-support copied to clipboard
Global variable (setGlobalVariable) not persisted/saved if the pre-request script throws an error
App Details:
Postman for Windows
Version 5.2.1
win32 6.3.9600 / x64
Issue Report:
- 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)
- 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.
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?
There are 3 ellements that are important for our collections to work properly:
- Skipping the request if a validation/consistency error is detected
- Saving some global variables to perform in-between execution validations and deletion protection
- 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.cancelRequestor equivalent in order to stop request execution from pre-request scripts.
Note: related to solution of #2392
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?