I was trying to get the path alias using for nodes using the following code.
Drupal::service('path.alias_manager')->getAliasByPath('/node/' . $nid);
It works for nodes that are already created, but it breaks when a new node is being added. I get the following error.
Uncaught PHP Exception SymfonyComponentRoutingExceptionInvalidParameterException: “Parameter “node” for route “entity.node.canonical” must match “d+” (“” given) to generate a corresponding URL.
Is there an alternative way to get the path alias for nodes that have not been saved yet?