WSO2 ESB does not return ERROR_MESSAGE, but it does inside ERROR_RESPONSE - wso2

I am implementing failure sequence for a service in WSO2 EBS, but it just never return ERROR_MESSAGE or other failure properties like ERROR_REASON.
However it does return this fields inside $ctx:ERROR_RESPONSE.
how come is this possible? any clue how to extract this fields outside ERROR_RESPONSE?
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="Failure" statistics="enable" trace="enable" xmlns="http://ws.apache.org/ns/synapse">
<log description="Log error details in case of failure" level="full">
<property name="MESSAGE" value="An unexpected error occurred."/>
<property expression="$ctx:ERROR_CODE" name="ERROR_CODE"/>
<property expression="$ctx:ERROR_RESPONSE" name="ERROR_RESPONSE"/>
<property expression="$ctx:ERROR_MESSAGE" name="ERROR_MESSAGE"/>
</log>
<payloadFactory description="Build payload with error information" media-type="json">
<format>
{
"status": "An unexpected error occurred.",
"code": $1,
"response": $2,
"message": $3,
}
</format>
<args>
<arg evaluator="xml" expression="$ctx:ERROR_CODE"/>
<arg evaluator="xml" expression="$ctx:ERROR_RESPONSE"/>
<arg evaluator="xml" expression="$ctx:ERROR_MESSAGE"/>
</args>
</payloadFactory>
<respond description="Respond back to client with custom error payload"/>
</sequence>

Related

How to add additional information in the response?

Scenario
I've integrated an external service in my API. In addition to the response from the external service, I want to add a few more JSON keys and values.
API
<?xml version="1.0" encoding="UTF-8"?>
<api context="/PhoneVerify" name="PhoneVerifi" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST">
<inSequence>
<payloadFactory media-type="xml">
<format>
<soapenv:Envelope xmlns:cmpa="http://XXX.XXX.com" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://object.pmd.com/xsd">
<soapenv:Header/>
<soapenv:Body>
<cmpa:verify>
<!--Optional:-->
<cmpa:userName>XXXXX</cmpa:userName>
<!--Optional:-->
<cmpa:passwd>XXXXX</cmpa:passwd>
<!--Optional:-->
<cmpa:request>
<!--Optional:-->
<xsd:cnic>$1</xsd:cnic>
<!--Optional:-->
<xsd:msisdn>$2</xsd:msisdn>
<!--Optional:-->
<xsd:transactionID>$3</xsd:transactionID>
</cmpa:request>
</cmpa:verify>
</soapenv:Body>
</soapenv:Envelope>
</format>
<args>
<arg evaluator="json" expression="$.cnic"/>
<arg evaluator="json" expression="$.msisdn"/>
<arg evaluator="json" expression="$.transactionID"/>
</args>
</payloadFactory>
<log category="DEBUG" level="full"/>
<header name="Action" scope="default" value="verify"/>
<send>
<endpoint>
<address format="soap11" uri="https://XXXXX.com/CMPA/services/CnicMsisdnPairing.CnicMsisdnPairingHttpsSoap11Endpoint/">
<suspendOnFailure>
<initialDuration>-1</initialDuration>
<progressionFactor>1</progressionFactor>
</suspendOnFailure>
<markForSuspension>
<retriesBeforeSuspension>0</retriesBeforeSuspension>
</markForSuspension>
</address>
</endpoint>
</send>
</inSequence>
<outSequence>
<property name="messageType" scope="axis2" type="STRING" value="application/json"/>
<payloadFactory media-type="json">
<format>
{
"status" : "success"
"response": "$1"
}</format>
<args>
<arg evaluator="json" expression="$.verifyResponse.return"/>
</args>
</payloadFactory>
<property name="messageType" scope="axis2" type="STRING" value="application/json"/>
<respond/>
</outSequence>
<faultSequence>
<payloadFactory media-type="json">
<format>
{
"status" : "failure"
}</format>
<args/>
</payloadFactory>
</faultSequence>
</resource>
</api>
Response from External Service
"verifyResponse":{
"return" : {
"#type":"ax21:Response",
"message":"Duplicate Transaction ID",
"responseCode":"08",
"status":"00"
}
}
Desired Response
{
"status": "success"
"response": {
"#type":"ax21:Response",
"message":"Duplicate Transaction ID",
"responseCode":"08",
"status":"00"}
}
Question
My exact question is that when I try to achieve the above problem, I'm getting the response as shown below (basically its wrapped in " "). How can I make achieved the desired response?
{
"status": "success"
"response": "{"#type":"ax21:Response","message":"Duplicate Transaction ID","responseCode":"08","status":"00"}"
}
The Payload factory is adding " " since you have defined the argument $1 within " ". If you modify the Payload factory as below you should be able to get the desired output.
<payloadFactory media-type="json">
<format>
{
"status" : "success",
"response": $1
}
</format>
<args>
<arg evaluator="json" expression="$.verifyResponse.return"/>
</args>
</payloadFactory>
I have tried the above in the latest Integration Studio(8.1.0), if the above solution doesn't work please specify the MI version you are using.

I got a problem in rest api project in wso2 integration studio

I Have this xml:
<?xml version="1.0" encoding="UTF-8"?>
<api context="/testapi" name="testapi" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="GET" url-mapping="/testapi">
<inSequence>
<property expression="get-property('query.param.qp')" name="qp1" scope="default" type="STRING"/>
<property expression="$ctx:query.param.qp" name="qp2" scope="default" type="STRING"/>
<property expression="$url:qp" name="qp3" scope="default" type="STRING"/>
<payloadFactory media-type="json">
<format> {"Method1":"$1",
"Method2":"$2",
"Method3":"$3"}</format>
<args>
<arg evaluator="xml" expression="get-property('qp1')"/>
<arg evaluator="xml" expression="get-property('qp2')"/>
<arg evaluator="xml" expression="get-property('qp3')"/>
</args>
</payloadFactory>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
when validating the code in the integration studio these problems appears:
Element 'format' cannot have character [children], because the type's content type is element-only. testapi.xml /test1/test1Configs/src/main/synapse-config/api line 9 XML Problem
The content of element 'faultSequence' is not complete. One of '{"http://ws.apache.org/ns/synapse":call, "http://ws.apache.org/ns/synapse":call-template, "http://ws.apache.org/ns/synapse":drop, "http://ws.apache.org/ns/synapse":log, "http://ws.apache.org/ns/synapse":loopback, "http://ws.apache.org/ns/synapse":property, "http://ws.apache.org/ns/synapse":propertyGroup, "http://ws.apache.org/ns/synapse":respond, "http://ws.apache.org/ns/synapse":send, "http://ws.apache.org/ns/synapse":sequence, "http://ws.apache.org/ns/synapse":store, "http://ws.apache.org/ns/synapse":conditionalRouter, "http://ws.apache.org/ns/synapse":filter, "http://ws.apache.org/ns/synapse":switch, "http://ws.apache.org/ns/synapse":validate, "http://ws.apache.org/ns/synapse":bean, "http://ws.apache.org/ns/synapse":class, "http://ws.apache.org/ns/synapse":pojoCommand, "http://ws.apache.org/ns/synapse":ejb, "http://ws.apache.org/ns/synapse":script, "http://ws.apache.org/ns/synapse":spring, "http://ws.apache.org/ns/synapse":enrich, "http://ws.apache.org/ns/synapse":makefault, "http://ws.apache.org/ns/synapse":header, "http://ws.apache.org/ns/synapse":payloadFactory, "http://ws.apache.org/ns/synapse":jsontransform, "http://ws.apache.org/ns/synapse":smooks, "http://ws.apache.org/ns/synapse":rewrite, "http://ws.apache.org/ns/synapse":xquery, "http://ws.apache.org/ns/synapse":xslt, "http://ws.apache.org/ns/synapse":datamapper, "http://ws.apache.org/ns/synapse":fastXSLT, "http://ws.apache.org/ns/synapse":cache, "http://ws.apache.org/ns/synapse":dblookup, "http://ws.apache.org/ns/synapse":dbreport, "http://ws.apache.org/ns/synapse":enqueue, "http://ws.apache.org/ns/synapse":event, "http://ws.apache.org/ns/synapse":throttle, "http://ws.apache.org/ns/synapse":transaction, "http://ws.apache.org/ns/synapse":aggregate, "http://ws.apache.org/ns/synapse":callout, "http://ws.apache.org/ns/synapse":clone, "http://ws.apache.org/ns/synapse":iterate, "http://ws.apache.org/ns/synapse":foreach, "http://ws.apache.org/ns/synapse":entitlementService, "http://ws.apache.org/ns/synapse":oauthService, "http://ws.apache.org/ns/synapse":builder, "http://ws.apache.org/ns/synapse":rule, "http://ws.apache.org/ns/synapse":bam, "http://ws.apache.org/ns/synapse":publishEvent, "http://ws.apache.org/ns/synapse":NTLM}' is expected. testapi.xml /test1/test1Configs/src/main/synapse-config/api line 3 XML Problem
The content of element 'outSequence' is not complete. One of '{"http://ws.apache.org/ns/synapse":call, "http://ws.apache.org/ns/synapse":call-template, "http://ws.apache.org/ns/synapse":drop, "http://ws.apache.org/ns/synapse":log, "http://ws.apache.org/ns/synapse":loopback, "http://ws.apache.org/ns/synapse":property, "http://ws.apache.org/ns/synapse":propertyGroup, "http://ws.apache.org/ns/synapse":respond, "http://ws.apache.org/ns/synapse":send, "http://ws.apache.org/ns/synapse":sequence, "http://ws.apache.org/ns/synapse":store, "http://ws.apache.org/ns/synapse":conditionalRouter, "http://ws.apache.org/ns/synapse":filter, "http://ws.apache.org/ns/synapse":switch, "http://ws.apache.org/ns/synapse":validate, "http://ws.apache.org/ns/synapse":bean, "http://ws.apache.org/ns/synapse":class, "http://ws.apache.org/ns/synapse":pojoCommand, "http://ws.apache.org/ns/synapse":ejb, "http://ws.apache.org/ns/synapse":script, "http://ws.apache.org/ns/synapse":spring, "http://ws.apache.org/ns/synapse":enrich, "http://ws.apache.org/ns/synapse":makefault, "http://ws.apache.org/ns/synapse":header, "http://ws.apache.org/ns/synapse":payloadFactory, "http://ws.apache.org/ns/synapse":jsontransform, "http://ws.apache.org/ns/synapse":smooks, "http://ws.apache.org/ns/synapse":rewrite, "http://ws.apache.org/ns/synapse":xquery, "http://ws.apache.org/ns/synapse":xslt, "http://ws.apache.org/ns/synapse":datamapper, "http://ws.apache.org/ns/synapse":fastXSLT, "http://ws.apache.org/ns/synapse":cache, "http://ws.apache.org/ns/synapse":dblookup, "http://ws.apache.org/ns/synapse":dbreport, "http://ws.apache.org/ns/synapse":enqueue, "http://ws.apache.org/ns/synapse":event, "http://ws.apache.org/ns/synapse":throttle, "http://ws.apache.org/ns/synapse":transaction, "http://ws.apache.org/ns/synapse":aggregate, "http://ws.apache.org/ns/synapse":callout, "http://ws.apache.org/ns/synapse":clone, "http://ws.apache.org/ns/synapse":iterate, "http://ws.apache.org/ns/synapse":foreach, "http://ws.apache.org/ns/synapse":entitlementService, "http://ws.apache.org/ns/synapse":oauthService, "http://ws.apache.org/ns/synapse":builder, "http://ws.apache.org/ns/synapse":rule, "http://ws.apache.org/ns/synapse":bam, "http://ws.apache.org/ns/synapse":publishEvent, "http://ws.apache.org/ns/synapse":NTLM}' is expected. testapi.xml /test1/test1Configs/src/main/synapse-config/api line 3 XML Problem
Cannot find the declaration of element 'project'. pom.xml /test1/test1CompositeExporter line 3 XML Problem
Referenced file contains errors (jar:file:/D:/WSO2-Integration-Studio-8.1.0-win32-x86_64/IntegrationStudio/plugins/org.wso2.integrationstudio.gmf.esb.diagram_8.1.0.202211111317.jar!/resources/schema/endpoint.xsd). For more information, right click on the message in the Problems View and select "Show Details..." testapi.xml /test1/test1Configs/src/main/synapse-config/api line 1 XML Problem
I try to build rest api with wso2 integration studio
You have to delete those Caracters "&#xd" from your JSON Payload as below :
<?xml version="1.0" encoding="UTF-8"?>
<api context="/testapi" name="testapi" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="GET" url-mapping="/testapi">
<inSequence>
<property expression="get-property('query.param.qp')" name="qp1" scope="default" type="STRING"/>
<property expression="$ctx:query.param.qp" name="qp2" scope="default" type="STRING"/>
<property expression="$url:qp" name="qp3" scope="default" type="STRING"/>
<payloadFactory media-type="json">
<format> {"Method1":"$1",
"Method2":"$2",
"Method3":"$3"}</format>
<args>
<arg evaluator="xml" expression="get-property('qp1')"/>
<arg evaluator="xml" expression="get-property('qp2')"/>
<arg evaluator="xml" expression="get-property('qp3')"/>
</args>
</payloadFactory>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>

WSO2 change reply to send to the caller

Sorry for my English, I'm using Google Translate to help myself.
I'm writing an API call to a web service. The call is ok and the api returns the response to the caller service, but the client wants that the output of the end-point must be changed from WSO2.
I added this outSequence to the api but the return is a returncode 202 without body.
Can someone help me please?
<outSequence>
<sequence key="">
<payloadFactory media-type="json">
<format>
{ "status": "1",
"message": $2
}
</format>
<args>
<arg evaluator="xml" expression="get-property('ERROR_CODE')"/>
<arg evaluator="xml" expression="get-property('ERROR_MESSAGE')"/>
<arg evaluator="xml" expression="get-property('ERROR_DETAIL')"/>
</args>
</payloadFactory>
<property name="messageType" scope="axis2" type="STRING" value="application/json"/>
<send/>
</sequence>
</outSequence>
I think you should try something like this instead
<inSequence>
<sequence key="">
<payloadFactory media-type="json">
<format>
{ "status": "1",
"message": $2
}
</format>
<args>
<arg evaluator="xml" expression="get-property('ERROR_CODE')"/>
<arg evaluator="xml" expression="get-property('ERROR_MESSAGE')"/>
<arg evaluator="xml" expression="get-property('ERROR_DETAIL')"/>
</args>
</payloadFactory>
<property name="messageType" scope="axis2" type="STRING" value="application/json"/>
<send/>
</inSequence>
<outSequence>
<property expression="json-eval($)" name="body-before-aggreg" scope="default" type="STRING"/>
<aggregate>
<completeCondition>
<messageCount max="-1" min="-1"/>
</completeCondition>
<onComplete aggregateElementType="root" expression="json-eval($)">
<!--get back from send body here in outsequence!-->
<respond/>
</onComplete>
</aggregate>
</outSequence>
<faultSequence>
<log level="custom">
<property name="text" value="An unexpected error occured for service"/>
<property expression="get-property('ERROR_MESSAGE')" name="message"/>
</log>
<respond/>
</faultSequence>
</resource>
</api>
please note that when you use send -> response messages will go through the Out mediator.

How can I get the soapenv content in WSO2?

In this case, how can I get the "MyString" content in WSO2 response?
Request:
<payloadFactory media-type="json">
<format>{
"Name" : "$1",
"group": "$3"
}
</format>
<args>
<arg evaluator="xml" expression="$ctx:Name"/>
<arg evaluator="xml" expression="$ctx:group"/>
</args>
</payloadFactory>
<call>
<endpoint key="ep_Server"/>
</call>
response:
Direction: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><text xmlns="http://ws.apache.org/commons/ns/payload">{"MyString":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVc2VySWQiOjEsIlN5c3RlbUlkIjoyMSwiUHJvcGVydHlJZCI6OSwiSXNBZG1pbiI6ZmFsc2UsIkNyZWF0ZURhdGVUaW1lIjoiMjAxNy0xMS0wOFQxMDoyMjoxMi45MDA3MjE4KzA4OjAwIn0.k6FyUGwXOAeC63oGsPWz8ttwo1LeDG3vnTbw7dJ18GY"}</text></soapenv:Body></soapenv:Envelope>
Try this.
<log>
<property name="MyString" expression="json-eval($.MyString)"></property>
</log>
Ref: https://docs.wso2.com/display/ESB500/JSON+Support
In your case the output is not json it's xml containing a text, usually it indicate that you're using text/plain type, I think the issue is there.
Moreover your output is not matching the sample code you've
Could you try to put the following before your call:
<property name="messageType" scope="axis2" type="STRING" value="application/json"/>
What is the content type that the service you're calling is returning? Is it valid? If not it could be that wso2 ei is by default considering it's text

WSO2 API manager - How to send Error/Fault message back to the Client from InSequence

I have created a REST API using WSO2 API Manager (StockQuoteService) and configured a back end SOAP based web service (converting REST to SOAP) from where it will be getting the data based on the URL template.
In the "In Sequence", I have used a Switch mediator to send the request to different back end endpoints based on incoming data whereas in the default scenario (when no case match), I want to send the error message back to the client that the "Input message is invalid".
I have tried using the Send mediator, Respond Mediator, Sequence Mediator but still no success (may be doing something wrong) as still I am getting "no response from server" error when I try to invoke the URL which doesn't match any case of Switch and goes to Default.
How can I send the Error/Fault message back to the client from In Sequence of WSO2 API Manager?
In my scenario, the input sequence I used the switch mediator and I invoke an operation or another, in the default option I create my failure response
<inSequence>
<switch xmlns:xsd="http://pharmacy.arce.org/xsd"
description=""
source="//xsd:desc">
<case regex="NATURAL">
<log description="Search Pharmacy" level="custom" separator=",">
<property name="STATUS" value="Search Pharmacy"/>
</log>
<payloadFactory media-type="xml">
<format>
<p:searchpharmacy xmlns:p="http://pharmacy.arce.org">
<ax22:pharmacy xmlns:ax22="http://pharmacy.arce.org">
<xs:desc xmlns:xs="http://pharmacy.arce.org/xsd">$1</xs:desc>
<xs:id xmlns:xs="http://pharmacy.arce.org/xsd">$2</xs:id>
<xs:latitude xmlns:xs="http://pharmacy.arce.org/xsd">$3</xs:latitude>
<xs:longitude xmlns:xs="http://pharmacy.arce.org/xsd">$4</xs:longitude>
</ax22:pharmacy>
</p:searchpharmacy>
</format>
<args>
<arg evaluator="xml" expression="//xsd:desc"/>
<arg evaluator="xml" expression="//xsd:id"/>
<arg evaluator="xml" expression="//xsd:latitude"/>
<arg evaluator="xml" expression="//xsd:longitude"/>
</args>
</payloadFactory>
<header name="To" scope="default" value="urn:searchpharmacy"/>
<log level="full" separator=",">
<property name="Mensaje" value="Cuerpo"/>
</log>
</case>
<case regex="EXPERIMENTAL">
<log description="Search Pharmacy Direction" level="custom" separator=",">
<property name="STATUS" value="Search Pharmacy Direction Request"/>
</log>
<payloadFactory media-type="xml">
<format>
<p:searchPhone xmlns:p="http://pharmacy.arce.org">
<ax22:pharmacy xmlns:ax22="http://pharmacy.arce.org">
<xs:desc xmlns:xs="http://pharmacy.arce.org/xsd">$1</xs:desc>
<xs:id xmlns:xs="http://pharmacy.arce.org/xsd">$2</xs:id>
<xs:latitude xmlns:xs="http://pharmacy.arce.org/xsd">$3</xs:latitude>
<xs:longitude xmlns:xs="http://pharmacy.arce.org/xsd">$4</xs:longitude>
</ax22:pharmacy>
</p:searchPhone>
</format>
<args>
<arg evaluator="xml" expression="//xsd:desc"/>
<arg evaluator="xml" expression="//xsd:id"/>
<arg evaluator="xml" expression="//xsd:latitude"/>
<arg evaluator="xml" expression="//xsd:longitude"/>
</args>
</payloadFactory>
<header name="Action" scope="default" value="urn:searchPhone"/>
<property name="SOAPAction" scope="transport" type="STRING" value=""/>
<log level="full" separator=",">
<property name="Data" value="Body"/>
</log>
</case>
<default>
<log description="Fault" level="custom" separator=",">
<property name="STATUS" value="Invoke fault "/>
</log>
<payloadFactory media-type="xml">
<format>
<rs:fault xmlns:rs="http://pharmacy.arce.org">
<rs:code>-1</rs:code>
<rs:type>Invocation error</rs:type>
<rs:message>No operation has been invoked</rs:message>
<rs:description>The value of the input parameter is not valid</rs:description>
</rs:fault>
</format>
<args/>
</payloadFactory>
<respond/>
</default>
</switch>
</inSequence>
Here is the default section
<default>
<log description="Fault" level="custom" separator=",">
<property name="STATUS" value="Invoke fault "/>
</log>
<payloadFactory media-type="xml">
<format>
<rs:fault xmlns:rs="http://pharmacy.arce.org">
<rs:code>-1</rs:code>
<rs:type>Invocation error</rs:type>
<rs:message>No operation has been invoked</rs:message>
<rs:description>The value of the input parameter is not valid</rs:description>
</rs:fault>
</format>
<args/>
</payloadFactory>
<respond/>
</default>
And the fault response
{"fault":{"code":-1,"type":"Invocation error","message":"No operation has been invoked","description":"The value of the input parameter is not valid"}}
You can find other scenarios here
http://harshcreationz.blogspot.com/2016/02/common-and-error-handling-sequences.html
This should work.
<payloadFactory media-type="json">
<format>
{
"error":"true",
"message":"error case"
}
</format>
</payloadFactory>
<property name="messageType" value="application/json" scope="axis2"/>
<respond/>
Nits
The error response as comments "no response from server", I receive it when I call the api from the test console that has the API Manager.
You can see this link
API Console Issue
To validate the response you expect.
Try calling from another tool such as SOAP UI or Postman, in my case I receive a fault response message with the structure defined.
This is the call from the console API
curl -X GET --header 'Accept: application/xml' --header 'Authorization: Bearer 465f1385-a120-3c19-ad22-c3057e744a3b' 'https://169.254.193.10:8252/getById/1.0.0/getEmployeeXML?Id=5'
For the call from another client in the header pass these values you have in the api call
Accept: application/json
Authorization: Bearer 465f1385-a120-3c19-ad22-c3057e744a3b'