Why my app crashing instead of sending `invalid url` error

i’m working on url shortener project. here i’m throwing an error if there is an error in dns.lookup method. but it crashes my app instead of going to catch block.

here is my app on glitch https://fcc-url-shortener-gskumawat.glitch.me/

app.post('/api/shorturl/new', async function(req, res){
  try{
    let {host} = await new URL(req.body.url);
    dns.lookup(host, function(err, family, address){
      if(err){
        throw  Error('Invalid Url');
      };
    });
    res.redirect('/');
  }
  catch(err){
    return res.json({
      error: err.message
    })
  }
})

I don’t think that express works properly with async functions like that by the way, there might be a few subtle issues with that

What message is node crashing with? If it’s similar to a situation I had it’ll be uncaught promise exception or something

I don’t think you can catch exceptions thrown in callbacks that easily as they happen after the try block is left, and it’s complicated a bit by the fact there’s the async keyword, which may be looking for rejected promises

I’d probably remove the async keyword and then use a promise chain directly, and rewrite dns.lookup to return a promise directly elsewhere