Array returning org.mozilla.javascript.NativeArray

Hi, i have a javascript policy with RaiseFault rule to bind my exceptions coming from the backend. I don't understand what i doing wrong because my array response is: org.mozilla.javascript.NativeArray. 

Here my js:

 

function parseErrorField(obj) {
    var errorParsed = {
      attribute: obj.field,
      messages: obj.defaultMessage
    };

    return JSON.parse(JSON.stringify(errorParsed));
}

if(errorContent.erros != null) {

  var errorFields = errorContent.erros.map(parseErrorField);

  context.setVariable("global.error.fields", errorFields);
}

 

 
Actual response:
 

 

{
    "error": {
        "id": "UUID",
        "code": "400",
        "description": "Bad request",
        "error_details": "org.mozilla.javascript.NativeArray"
    }
}

 

 
Excepted response

 

{
    "error": {
        "id": "UUID",
        "code": "400",
        "description": "Bad request",
        "error_details": [
           {
              "attribute": "field",
              "messages": "error"
           },
           ...
         ]
    }
}

 

1 1 785
1 REPLY 1

ya, I can't quite get what you're doing. This code 

context.setVariable("global.error.fields", errorFields);

...sets a variable, but then you showed "actual response", which.... doesn't have any relation to that variable. So I don't know how the code you're showing relates to the output you're seeing. 

I think maybe the problem might be here:

if(errorContent.erros != null) {
  var errorFields = errorContent.erros.map(parseErrorField);
  context.setVariable("global.error.fields", errorFields);
}

You don't say how "errorContent.erros" gets set.  Presumably something is putting a value there. I think the "NativeArray" output is telling you that there is an array there that the JS policy cannot deal with.   Maybe there is some other policy setting that value. 

Sometimes you can get that when you read message.header.HEADERNAME.  When the header is  multi-valued  (has commas), then you will get a collection.  a NativeArray.  If you want the string value, with all the commas, you can retrieve message.header.HEADERNAME.values.string . That won't be an array, it will be just a string with any commas within it. 

But maybe your variable is being set in some other way.  Regardless, your JS is getting a NativeArray and the code isn't handling it properly. Investigate that, that seems to be the source of the problem.