# Error handling and getting a name

**URL:** <https://discourse.slimframework.com/t/error-handling-and-getting-a-name/3456>\
**Category:** Questions\
**Created:** [September 19, 2019, 6:58pm UTC](https://discourse.slimframework.com/t/error-handling-and-getting-a-name/3456 "2019-09-19T18:58:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unnamed123](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@unnamed123](https://discourse.slimframework.com/u/unnamed123)\
**Post date:** [September 19, 2019, 6:58pm UTC](https://discourse.slimframework.com/t/error-handling-and-getting-a-name/3456/1 "2019-09-19T18:58:03Z")

</div>

1. How to handle errors 404, 500, 409 etc.?  
In version 3, this happened through containers. How about 3?  
**So in version 3:**

```auto
$c = new \Slim\Container();

$c['notFoundHandler'] = function ($c) {
    return function ($request, $response) use ($c) {
        return $response->withStatus(404)
            ->withHeader('Content-Type', 'text/html')
            ->write('Page not found');
    };
};

$app = new \Slim\App($c);

```

**How about 4?**

1. And does anyone know how to get the name of the router that was executed?  
For example:  
`$app->get('/', Home::class . ':get')->setName('home');`

**And I need to get a name**

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [September 19, 2019, 7:22pm UTC](https://discourse.slimframework.com/t/error-handling-and-getting-a-name/3456/2 "2019-09-19T19:22:25Z")

</div>

Hi @unnamed123

1. I’m sure you’ll find the answer here 🙂  
[http://www.slimframework.com/docs/v4/middleware/error-handling.html](http://www.slimframework.com/docs/v4/middleware/error-handling.html)

2. Try this:

```php
$routeName = \Slim\Routing\RouteContext::fromRequest($request)->getRoute()->getName();

```

---

<div class="post-metadata">

**Author:** ![unnamed123](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@unnamed123](https://discourse.slimframework.com/u/unnamed123)\
**Post date:** [September 20, 2019, 10:49am UTC](https://discourse.slimframework.com/t/error-handling-and-getting-a-name/3456/3 "2019-09-20T10:49:42Z")

</div>

Thanks!

I want to use RouteContex :: fromRequest () in the middleware, but the error is:

```
Fatal error: Uncaught RuntimeException: Cannot create RouteContext before routing has been completed in G:\Program\OSPanel\domains\Slim4\vendor\slim\slim\Slim\Routing\RouteContext.php on line *30*
RuntimeException: Cannot create RouteContext before routing has been completed in G:\Program\OSPanel\domains\Slim4\vendor\slim\slim\Slim\Routing\RouteContext.php on line *30*

```

How to fix it?

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [September 20, 2019, 12:44pm UTC](https://discourse.slimframework.com/t/error-handling-and-getting-a-name/3456/4 "2019-09-20T12:44:39Z")

</div>

Then make sure that you have added the RoutingMiddleware:

```php
$app = AppFactory::create();

// Add Routing Middleware
$app->addRoutingMiddleware();

// ...

$app->run();

```
