1

I have a bit of code that loops through a list of URLs and fetches them using needle, I am having a problem as it seems if one of the URLs is malformed needle throws a URIError that stops the execution.

What I want is for the bad URL to be marked but the loop continues, I have the following code that include a try-catch block but it still throws an error

URIError: URI malformed
    at decodeURIComponent (native)
    at parseSetCookieString (node_modules/needle/lib/cookies.js:44:35)
    at node_modules/needle/lib/cookies.js:54:18
    at Array.reduce (native)
    at Object.parseSetCookieHeader [as read] (node_modules/needle/lib/cookies.js:53:17)
    at ClientRequest.<anonymous> (node_modules/needle/lib/needle.js:418:30)
    at ClientRequest.g (events.js:199:16)
    at ClientRequest.emit (events.js:107:17)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:426:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:111:23)

The loop looks like this

for (var i =0; i < items.length; i++)
  {
    var url = items[i].link;
    var options = {};
    (function(inner_url){
      try {
        needle.get(inner_url, options, function(err, res) {
          if(err) {
            console.log(err)
          } else {
            console.log('success')
          }
        })
      } catch (URIError) {
        console.log(URIError)
      }
  })(url)
  }

Any pointers?

Thanks

Haddad
  • 369
  • 1
  • 2
  • 9

1 Answers1

1

When you use asynchronous code try/catch will not help you.

For example

try {var a = a.b.c.d.s}catch(e){} will work.

But try{requestToSomeURL or any async operation}catch(e){} will not.

The reason for that is when you run something async JavaScript done with this piece of code(context is lost) and go to next one. When event loop triggers the callback function, it gets exception and show it to you.

To fix this you need to add error handling into needle.get method. In that case your callback function will pass all errors.

Or if this is a third-party library you need to check your url before doing GET operation.

Check also this answer for more info

http://ruben.verborgh.org/blog/2012/12/31/asynchronous-error-handling-in-javascript/

Should async function never ever throw?

Hope this helps.

Community
  • 1
  • 1
Mykola Borysyuk
  • 3,373
  • 1
  • 18
  • 24
  • Thanks for the answer, unfortunately needle is a third party module that I am not maintaining. May raise an issue with the developer though – Haddad Sep 22 '16 at 13:00
  • Well then as i mentioned..Just check your URL before doing request. And if URL has some bad values just dont do request.. This will prevent those kind of issues. – Mykola Borysyuk Sep 23 '16 at 08:40