2

Created a soap api with the node-soap library but when i try to throw a fault using

throw {
    Fault: {
      Code: {
        Value: 'soap:Sender',
        Subcode: { value: 'rpc:BadArguments' }
      },
      Reason: { Text: 'Processing Error' }
    }
  };

as descibed in the library i get a

UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)

this is what i am currently doing

                     ssm.decrypt(args.request, password, base_key)
                        .then(function (res) {
                            console.log(res)
                            parseString(res, async function (err, result) {
                                if (err) {
                               //the throws causes the error
                                    throw {
                                        Fault: {
                                            error: {
                                                data: {
                                                    error
                                                } //Error object from catch
                                            }
                                        }

                                    };
                                } else {
                                 //some code
                                }

thanks

nipek
  • 810
  • 1
  • 9
  • 22

2 Answers2

0

Throwing an error inside the async function

parseString(res, async function (err, result)...

rejects the promise returned by the async function - for which there is no catch handler. If parseString calls its callback synchronously, you could just remove the async declaration leaving the call as

 parseString(res, function (err, result)...

If parseString is asynchronous, however, it needs to be promisified so errors can be worked into the surrounding promise chain. As an untested example:

function doParseString( res) {
    return new Promise( function( resolve, reject) {
        parseSting( res, function( err, result) {
             err ? reject( err) : resolve( result);
        });  
    });
}

which could be used along the lines of

ssm.decrypt(args.request, password, base_key)
.then( doParseString)
.then( function  (result) {
     // some code
 })
.catch( console.log);   // do something with the error
traktor
  • 17,588
  • 4
  • 32
  • 53
0

Thanks, guys for your help but I got the issue

if an async function is been used for the soap api been implemented with node-soap

communication need to be done via callbacks

i changed the throw to

                                                   callback({
                                                        Fault: {
                                                            error: {
                                                                data: {
                                                                    error
                                                                } 
                                                            }
                                                        }

                                                    });

and it works perfectly

i found it here node-soap doc

nipek
  • 810
  • 1
  • 9
  • 22
  • Have you checked the `Client.methodAsync(args, options)`[example](https://www.npmjs.com/package/soap#example)? I'm trying to use it, but I guess that callbacks are intended for the "regular" example, the async version is supposed to return a `Promise` (ideally) – funder7 May 30 '23 at 23:12