For this we need to add additional sequence which convert message into JSON. Then we need to add that sequence to auth failure sequence so authentication faliure message will be convert and sent to client.
Add convert sequence to _auth_failure_handler_
convert sequence
Add convert sequence to _auth_failure_handler_
<sequencename="_auth_failure_handler_"xmlns="http://ws.apache.org/ns/synapse">
<sequencekey="convert"/>
</sequence>
convert sequence
<sequencexmlns="http://ws.apache.org/ns/synapse"name="convert">
<sequencekey="_build_"/>
<filtersource="get-property('MESSAGE_FORMAT')"regex="soap1[1-2]">
<then>
<propertyname="SOAP_FAULT_CODE"value="Server"/>
<makefault>
<codeexpression="$ctx:SOAP_FAULT_CODE"/>
<reasonexpression="$ctx:ERROR_MESSAGE"/>
</makefault>
</then>
<else>
<payloadFactory>
<format>
<am:faultxmlns:am="http://wso2.org/apimanager">
<am:code>$1</am:code>
<am:type>Status report</am:type>
<am:message>Runtime Error</am:message>
<am:description>$2</am:description>
</am:fault>
</format>
<args>
<argexpression="$ctx:ERROR_CODE"/>
<argexpression="$ctx:ERROR_MESSAGE"/>
</args>
</payloadFactory>
<filtersource="$axis2:HTTP_METHOD"regex="^(?!.*(POST|PUT)).*$"></filter>
</else>
</filter>
<filterxpath="$ctx:CUSTOM_HTTP_SC">
<then>
<propertyname="HTTP_SC"expression="$ctx:CUSTOM_HTTP_SC"scope="axis2"/>
</then>
<else>
<propertyname="HTTP_SC"value="500"scope="axis2"/>
</else>
</filter>
<classname="org.wso2.carbon.apimgt.usage.publisher.APIMgtFaultHandler"/>
<propertyname="RESPONSE"value="true"/>
<headername="To"action="remove"/>
<propertyname="NO_ENTITY_BODY"scope="axis2"action="remove"/>
<propertyname="ContentType"scope="axis2"action="remove"/>
<propertyname="Authorization"scope="transport"action="remove"/>
<propertyname="Host"scope="transport"action="remove"/>
<propertyname="Accept"scope="transport"action="remove"/>
<propertyname="X-JWT-Assertion"scope="transport"action="remove"/>
<propertyname="messageType"value="application/json"scope="axis2"/>
<send/>
</sequence>