2
votes

I am using Mule 4 and I have a Value Error Selector when the payload is a string instead of having the following structure:

{"status": 0, "content": {"status": "201",...........

I am using a set variable in Mule 4 giving this value:

<set-variable value="#[if (payload.content? and payload.content.status? and payload.content.status &gt;= 200) payload.content.status else 500]" doc:name="Set Status" doc:id="e9ce4fd1-9664-4401-b9a2-e47913b689af" variableName="httpStatus"/>

the condition I am using is :

if (payload.content? and payload.content.status? and payload.content.status >= 200) payload.content.status else 500

but sometimes the server can answer a string like 'Non Authorized', then payload.content doesn't exist, is just a string.

Set Variable is giving me this error when the payload is an string

"\"You called the function 'Value Selector' with these arguments: \n 1: Binary (\"Richiesta Non Autorizzata\" as Binary {encoding: \"UTF-8\", mediaType: \"text/ht...)\n 2: Name (\"content\")\n\nBut it expects one of these combinations:\n (Array, Name)\n (Array, String)\n (Date, Name)\n (DateTime, Name)\n (LocalDateTime, Name)\n (LocalTime, Name)\n (Object, Name)\n (Object, String)\n (Period, Name)\n (Time, Name)\n\n1| if (payload.content? and payload.content.status? and payload.content.status >= 200) payload.content.status else 500\n ^^^^^^^^^^^^^^^\nTrace:\n at main (line: 1, column: 5)\" evaluating expression: \"if (payload.content? and payload.content.status? and payload.content.status >= 200) payload.content.status else 500\".",
2

2 Answers

1
votes

Use payload.content.status default {} or you can default it to a string or some other value that you can compare later and work with.

1
votes

The best way I can think of is instead of checking if the field exists check if the value is Object then you can make sure .status is going to work

if (payload.content is Object and payload.content.status? and payload.content.status >= 200) 
    payload.content.status 
else 500