首页 文章

节点 - 在promise catch块上抛出SOAP错误

提问于
浏览
0

我正在使用 SOAP 库开发 SOAP Server in NodeJ S(v6.9.4) . 我必须从Postgres数据库请求一些数据 . 我选择使用 pg-promise lib .

SAOP服务实现如下:

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
      }
    };
  });
}

如果在数据库连接/请求期间出错,我需要返回SOAP Fault . 在SOAP lib上,您可以通过throwing a new Fault object执行此操作 .

在这个例子中,我想把它扔在catch块中 . 我知道这不是正确的方法,我正面临这个问题:

UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:3):[object Object]

怎么能扔我的 SOAP Fault exception in the main service function . 我尝试了setTimeout解决方案没有成功 . 承诺是PG查询的好解决方案吗?

1 回答

  • 0

    我通过使用soap lib提供的 soap callback function (cb)找到了解决方案 . 以下代码应在Async服务实现下返回Soap Fault异常:

    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
          }
        });
      });
     }
    

相关问题