0

I am developing an SOAP Server in NodeJS (v6.9.4) using the SOAP library. I have to request some data from a Postgres database. I choose to use the pg-promise lib.

SAOP Services are implemented like this :

TestConnection: function (args, cb, headers, req) {
db.any("select * from users where active=$1", [true])
  .then(function (data) {
    return {};
  })
  .catch(function (error) {
    throw {
      Fault: {
        faultcode: faultCode,
        faultstring: faultString,
        detail: {
          "ns:FaultInformation": {
            "ns:FaultCode": detailedFaultCode,
            "ns:FaultText": detailedFaultText
          }
        },
        statusCode: 500
      }
    };
  });
}

In case of error during the database connection/request, I need to return a SOAP Fault. On the SOAP lib, you can do this by throwing a new Fault object.

In this example, I want to throw it in the catch block. I know it's not the right way to do this and I am facing this issue :

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): [object Object]

How can I throw my SOAP Fault exception in the main service function. I tried without success the setTimeout solution. Is the promise a good solution for PG queries ?

Spawnrider
  • 1,727
  • 1
  • 19
  • 32

1 Answers1

0

I found a solution myself by using the soap callback function (cb) provided by the soap lib. The following code should return a Soap Fault exception under an Async service implementation :

TestConnection: function (args, cb, headers, req) {
db.any("select * from users where active=$1", [true])
  .then(function (data) {
    cb({});
  })
  .catch(function (error) {
    cb({
      Fault: {
        faultcode: faultCode,
        faultstring: faultString,
        detail: {
          "ns:FaultInformation": {
            "ns:FaultCode": detailedFaultCode,
            "ns:FaultText": detailedFaultText
          }
        },
        statusCode: 500
      }
    });
  });
 }
Spawnrider
  • 1,727
  • 1
  • 19
  • 32