ApigeeX- split function support in Javascript

Hello Everyone,

I am building a generic error handler in ApigeeX. I am trying to catch the OASValidation policy errors and format them in proper JSON structure. Below is the code snippet

 

 

var OASFailure = context.getVariable("OASValidation.OSV-OASValidation.failed");
var errorDetail = "";
print(OASFailure);
if(faultName == "Failed"){
    if(OASFailure){
    print("inside OAS validation loop");
    const oasFaultCause = context.getVariable("OASValidation.OSV-OASValidation.fault.cause");
    var oasFaultCauseSplit = oasFaultCause.split("ERROR - ");
    for (var i=1; i<oasFaultCauseSplit.length; i++) {
     errorDetail = errorDetail + i + ". "+  JSON.stringify(split[i]) + "\n" ;
     }
//    errorDetail = "Invalid payload.Please check your payload or url";
     print(errorDetail);
    errorMessage = "Bad Request";
    statusCode = "400";
    code = "400";
    reasonPhase = "Bad Request";
	}else {
	errorMessage = "Internal Server Error";
	errorDetail = "Service Error - The server encountered an error while attempting to fulfill the request.";
	statusCode = "500";	
	code = "500";
	reasonPhase = "Internal Server Error";
}
}

 

 

Below is the ERROR sample when OASValidation policy fails for any mandatory request fields/invalid resource path.

 

 

OASValidation OSV-OASValidation with resource "oas://OpenAPI-Spec-Validation.json": failed with reason: "[ERROR - Instance failed to match exactly one schema (matched 0 out of 2): [/oneOf/0: Object has missing required properties (["firstName"]), /oneOf/1: Object has missing required properties (["lastName"])] ERROR - [Path '/supplier'] Object has missing required properties (["Id"]): [] ERROR - Object has missing required properties (["address"]): []]"

 

 

 However when the proxy is called, Javascript policy fails with below runtime error

Execution of Javascript-1 failed with error: Javascript runtime error: "ReferenceError: "split" is not defined. (Javascript-1.js:79)".

Can anyone confirm if split function is supported in Javascript policy on APigeeX?I saw couple of thread on forum where the posters were able to use split function in their javascripts.Not sure where I am going wrong. Please help.

Solved Solved
2 2 118
1 ACCEPTED SOLUTION

The JS you showed is not the full JavaScript source, so the line numbers in the error message don't match up with the line numbers in the source you showed. But the error is complaining about "split". Based on code inspection, I think the runtime error is occurring here: 

 

if(faultName == "Failed"){
    if(OASFailure){
    print("inside OAS validation loop");
    const oasFaultCause = context.getVariable("OASValidation.OSV-OASValidation.fault.cause");
    var oasFaultCauseSplit = oasFaultCause.split("ERROR - ");
    for (var i=1; i<oasFaultCauseSplit.length; i++) {
     // LOOK HERE --->>
     errorDetail = errorDetail + i + ". "+  JSON.stringify(split[i]) + "\n" ;
     // <<-----
    } 

 

You have a snip like JSON.stringify(split[i]), and I don't think split is defined. Maybe you mean oasFaultCauseSplit.

View solution in original post

2 REPLIES 2

The JS you showed is not the full JavaScript source, so the line numbers in the error message don't match up with the line numbers in the source you showed. But the error is complaining about "split". Based on code inspection, I think the runtime error is occurring here: 

 

if(faultName == "Failed"){
    if(OASFailure){
    print("inside OAS validation loop");
    const oasFaultCause = context.getVariable("OASValidation.OSV-OASValidation.fault.cause");
    var oasFaultCauseSplit = oasFaultCause.split("ERROR - ");
    for (var i=1; i<oasFaultCauseSplit.length; i++) {
     // LOOK HERE --->>
     errorDetail = errorDetail + i + ". "+  JSON.stringify(split[i]) + "\n" ;
     // <<-----
    } 

 

You have a snip like JSON.stringify(split[i]), and I don't think split is defined. Maybe you mean oasFaultCauseSplit.

Ah yes! indeed it has to be oasFaultCauseSplit. Seeing the error i was just focussing on the split fiunction on line 5.

Thanks @dchiesa1  for the help as always.