Azure Logic App SAP Connector: troubleshooting 504 BadGateway "Request to SAP server timed out."

Today while testing upcoming functionality for the Azure Logic App SAP Connector I was feeding semi-random input to it and generated the following error which cause is not obvious at first hand:

     "body": {
        "error": {
            "code": 504,
            "source": "logic-apis-df.azure-apim.net",
            "clientRequestId": "f45788bc-5193-4691-8911-5c18d95b995e",
            "message": "BadGateway",
            "innerError": {
                "status": 504,
                "message": "Request to SAP server timed out.",
                "source": "sap-sumikum-logic-cp-df-scus.logic-ase-df.p.azurewebsites.net"
            }
        }
    }

Going to the On-Premises Data Gateway and exporting the logs (see /en-us/power-bi/service-gateway-onprem-tshoot#tools-for-troubleshooting), I spotted the following error details in the latest log file (actual IP addresses have been masked):


[From the files contained in the logs, GatewayYYYYMMDD.nnnnnnnn.log will contain the Logic App calls trace while GatewayConfigurator* contains local machine trace of your use of the configuration GUI application.]

   TemplateMessage: Error processing XML for the LOB Operation

GatewayPipelineErrorCode=DM_GWPipeline_Gateway_XmlPropertyParseException --->
Inner exception chain: Microsoft.ServiceModel.Channels.Exception.ConnectionException > SAP.Middleware.Connector.RfcCommunicationException
Microsoft.ServiceModel.Channels.Exception.ConnectionException: 
LOCATION    CPIC (TCP/IP) on local host with Unicode
ERROR       partner 'XXX.XXX.XXX.XXX:33323' not reached
TIME        Wed May 09 09:06:46 2018
RELEASE     721
COMPONENT   NI (network interface)
VERSION     40
RC          -10
MODULE      nixxi.cpp
LINE        3289
DETAIL      NiPConnect2: XXX.XXX.XXX.XXX:33323
SYSTEM CALL connect
ERRNO       10060
ERRNO TEXT  WSAETIMEDOUT: Connection timed out
COUNTER     2
 ---> SAP.Middleware.Connector.RfcCommunicationException: 
LOCATION    CPIC (TCP/IP) on local host with Unicode
ERROR       partner 'XXX.XXX.XXX.XXX:33323' not reached
TIME        Wed May 09 09:06:46 2018
RELEASE     721
COMPONENT   NI (network interface)
VERSION     40
RC          -10
MODULE      nixxi.cpp
LINE        3289
DETAIL      NiPConnect2: XXX.XXX.XXX.XXX:33323
SYSTEM CALL connect
ERRNO       10060
ERRNO TEXT  WSAETIMEDOUT: Connection timed out
COUNTER     2

   at SAP.Middleware.Connector.CpicConnection.ConnectAsClient(Dictionary`2 options, Boolean forRepository)
   at SAP.Middleware.Connector.RfcConnection.ConnectAsClient(RfcDestination destination, Boolean forRepository)
   at SAP.Middleware.Connector.RfcConnectionPool..ctor(RfcDestination destination, Boolean forRepositoryCalls)
   at SAP.Middleware.Connector.RfcConnectionPool.GetPool(RfcDestination destination, Boolean forRepository, Boolean create)
   at SAP.Middleware.Connector.RfcDestination.GetClient(Boolean forRepository)
   at SAP.Middleware.Connector.RfcDestination.SetAttributeValues(RfcSystemAttributes attribs)
   at SAP.Middleware.Connector.RfcDestination.get_SystemAttributes()
   at Microsoft.Adapters.SAP.NCo.RfcClientConnection.Open()
   at Microsoft.Adapters.SAP.SapConnection.OpenNCoConnection()
   at Microsoft.Adapters.SAP.SapConnection.Open(TimeSpan timeout)
   --- End of inner exception stack trace ---

So the timeout from Logic App reaching the back-end is not a gateway issue per say, it is the SAP system that isn't responding to the hail from SAP NCo's client. But why? While the RfcCommunicationException isn't specific, the hint is in the error string it provides:

 partner 'XXX.XXX.XXX.XXX:33323' not reached

33323 was the wrong port number. The test SAP Application Server I was using is actually at 3332, not 33323.

In conclusion, when you see a BadGateway error, don't assume there is an issue with the gateway service. Review the detailed error message and if needed go inspect the OPDG logs. The error may be with the SAP connection configuration passed to the gateway.