Problem with 404 code status and custom 404 page

So. I want create custom 404 page with my template.
In my template I use twig function path().

If I pass to my 404 page I get exception

What is this behavior for?

it prevents me from using a nice 404 template with links.

I think in this case it would be good practice to use the current url.

Basically, this will work (for generateUriFromResult):

...
    if ($result->isFailure()) {
       return $_SERVER['PATH_INFO'] ?? '/';
    }
...