I have this piece of code written in Application/view/layout/layout.phtml for adding a link which will be pointing to current page. This I added for debugging purpose for refreshing the current page.
<li class="active"><a href="<?php echo $this->url() ?>"><?php echo $this->translate('Refresh') ?></a></li>
This is working fine as long as I don't invalid route (404 error). I get below fatal error
Fatal error: Uncaught exception 'Zend\View\Exception\RuntimeException' with message 'No RouteMatch instance provided'
I wan't to handle this by adding a condition to check if its a 404 error or an invalid route before trying to render the url. I am not sure how to do it. I tried to look the source code of Zend\View\View class and ViewModel class to see if there is a way I can get error code or something which can tell its a 404 error.
Edit:
As a last resort I have adding the try catch block like below which is working fine but want to check if there is any elegant way
<?php
try{
$url = $this->url();
?>
<li class="active"><a href="<?php echo $url ?>"><?php echo $this->translate('Refresh ') ?></a></li>
<?php
} catch (Exception $ex) {
}
?>