Hello
I have a JS policy to prepare a backend call with advanced mapping.
To manage errors, I implemented the solution suggested here by @williamking .
However, to provide to the caller a better experience with meaningful response message in case of error (missing mandatory fields, etc..), I define more than one try catch section, like :
try {
source = JSON.parse(context.proxyRequest.content);
} catch (e) {
setErrorOn("400","400.1","Received content is not JSON or not valid","BadRequest");
}
try {
anotherCheckOrTransformation(source);
catch(e) {
setErrorOn("400","400.2","Error description about transformation","BadRequest")
}
The problem is: if the first “step” failed, it is catched, managed … but the code continue to run.
I added a flag and conditions, but I do not like this option…
var noError = true;
if (noError) {
try {
source = JSON.parse(context.proxyRequest.content);
} catch (e) {
setErrorOn("400","400.1","Received content is not JSON or not valid","BadRequest");
noError = false
}
}
if (noError) {
try {
anotherCheckOrTransformation(source);
catch(e) {
setErrorOn("400","400.2","Error description about transformation","BadRequest")
noError = false;
}
}
Do you know how I could interrupt my code execution (right after line 4 of my first example)?
Thanks