RaiseFault policy is not picking up the variable set in javascript

RaiseFault policy is not picking up the variable set in javascript.

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RaiseFault continueOnError="false" enabled="true" name="RF-transformationError">
  <DisplayName>RF-transformationError</DisplayName>
  <Properties/>
  <FaultResponse>
    <Set>
      <Headers/>
      <Payload contentType="application/json">
      {error.content} 
      </Payload>
      <StatusCode>{error.status.code}</StatusCode>
      <ReasonPhrase>{error.reason.phrase}</ReasonPhrase>
    </Set>
  </FaultResponse>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</RaiseFault>

 

// Javascript Policy

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Javascript continueOnError="false" enabled="true" timeLimit="5000" name="JS-genericTransformation">
  <DisplayName>JS-genericTransformation</DisplayName>
  <Properties/>
  <ResourceURL>jsc://genericTransformation.js</ResourceURL>
</Javascript>

 

// genericTransformation.js

 

var payload = {
  "request": {
    "url": context.getVariable('request.path'),
    "method": context.getVariable('request.verb'),
    "payload": context.getVariable('request.content')
  }
}

var myRequest = new Request();
myRequest.body = JSON.stringify(payload);
myRequest.headers['Content-Type'] = 'application/json';
myRequest.url = "https://" + context.getVariable('targetHost') + "/transform";
myRequest.method = 'POST';

// Define the callback function and process the response from the GET API request
function onComplete(response, error) {
    // Check if the HTTP request was successful
    if (response.status == 200) {
      context.setVariable('request.content', response.content);
     } else {
       setErrorResponse(response.status, "transform failed", "transform failed", "tranform failed");
     }
}


function setErrorResponse(errorStatus, errorReason, errorMessage, errorDetail) {
	var errorContent = "";
	var json = {};
	json.Error = {};
	json.Error.Message = errorMessage;
	json.Error.Detail = errorDetail;

	errorContent = JSON.stringify(json);

	context.setVariable("error.status.code", errorStatus);
	context.setVariable("error.reason.phrase", errorReason);
	context.setVariable("error.header.Content-Type", "application/json");
	context.setVariable("error.content", errorContent);
  context.setVariable("is_fault", true);
}

httpClient.send(myRequest, onComplete);

 

// SharedFlow

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SharedFlow name="default">
  <Step>
    <Name>JS-genericTransformation</Name>
  </Step>
  <Step>
    <Name>RF-transformationError</Name>
    <Condition>is_fault = true</Condition>
  </Step>
</SharedFlow>

 

Solved Solved
0 1 144
1 ACCEPTED SOLUTION

First I tried https://www.googlecloudcommunity.com/gc/Apigee/Correct-Way-to-Return-Error-from-JavaScript-Policy/m-... which sets error variable which didn't work and then I was setting attribute fault which also didn't work.

When i set on custom variable it worked. Seems like both are reserved variable and cannot be  set.

View solution in original post

1 REPLY 1

First I tried https://www.googlecloudcommunity.com/gc/Apigee/Correct-Way-to-Return-Error-from-JavaScript-Policy/m-... which sets error variable which didn't work and then I was setting attribute fault which also didn't work.

When i set on custom variable it worked. Seems like both are reserved variable and cannot be  set.