# Getting Request Attributes in Error Handler

**URL:** https://discourse.slimframework.com/t/getting-request-attributes-in-error-handler/1332
**Category:** Questions
**Created:** [March 27, 2017, 5:49pm UTC](https://discourse.slimframework.com/t/getting-request-attributes-in-error-handler/1332 "2017-03-27T17:49:40Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![LosLobos](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/loslobos/32/142_2.png) [@LosLobos](https://discourse.slimframework.com/u/LosLobos)
#### Post date: [March 27, 2017, 5:49pm UTC](https://discourse.slimframework.com/t/getting-request-attributes-in-error-handler/1332/1 "2017-03-27T17:49:40Z")

</div>

Hello Folks,

I’ve implemented Monolog on my Slim Application and I would to keep record of the Users whenever some Exception or Error occurs. ( Not only Users but some other data about the request that come from my Database ).

I have my Custom Error Handler that does all the formatting I need and trigger the logger. But I having trouble getting for example User data.

Currently there is a Middleware that retrieve user information and store it in Request Attributes.

**Example: UserMiddleware**

```
$newRequest = $request->withAttributes($userData);

$newResponse = $next($newRequest,$response);

```

and then on the next Route or Middleware I just have to: `$request->getAttributes()['user']` and all my User data is there for use.

Lets say that **UserMiddleware** have completed its task without Error and moved to the **Login Route** and then something bad happened. That would lead to an Exception and it’ll be handler by **errorHandler**.

The thing is I cannot do the same in the Error Handler as its not the same **$request** object I think.

**Example:**

```
   $this->container['errorHandler'] = function (ContainerInterface $container) {
                return function (Request $request, Response $response, Exception $exception) use ($container) {
                      
                      // This return an Empty Array.
                      $requestAttr = $request->getAttributes();

                     // This isn't even possible.
                      $userData = $requestAttr['user];

         };
   };

```

**EDITED:**

Currently I’m passing the Attributes to the **Container** everytime that setAttributes is called so that in the Handler I can retrieve all type of data.

**Example: On Controller**

```
// Just an Example.
public function setRequestAttributes($data, $replace = false){
     $this->requestInstance = $this->requestInstance->withAttributes($data)
     $this->getInterfaceContainer('Attributes') = $this->requestInstance->getAttributes();
}

```

**Example: On Handler**

```
 $requestAttributes = $container->get('Attributes');

```

Is there any way better or safer to do that ? This is the only one I got.

Thanks,  
LosLobos.

---

<div class="post-metadata">

### Author: ![MathMarques](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/mathmarques/32/501_2.png) [@MathMarques](https://discourse.slimframework.com/u/MathMarques)
#### Post date: [March 28, 2017, 2:43am UTC](https://discourse.slimframework.com/t/getting-request-attributes-in-error-handler/1332/2 "2017-03-28T02:43:35Z")

</div>

If you take a look on [this code](https://github.com/slimphp/Slim/blob/3.x/Slim/App.php#L626) you will see how Slim handle erros.  
But talking about your problem, you can just inject the request/response on your `Exception` like [Slim/Exception/MethodNotAllowedException](https://github.com/slimphp/Slim/blob/3.x/Slim/Exception/MethodNotAllowedException.php) (that extends [SlimException](https://github.com/slimphp/Slim/blob/3.x/Slim/Exception/SlimException.php)) do.  
Then you can do something like:

```php
throw new MyException($request, $response, $anotherArgument, ...)

```

And on your error handler

```php
$this->container['errorHandler'] = function (ContainerInterface $container) {
                return function (Request $request, Response $response, Exception $exception) use ($container) {
                     $requestAttr = $exception->getRequest()->getAttributes();
         };
   };

```

---

<div class="post-metadata">

### Author: ![LosLobos](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/loslobos/32/142_2.png) [@LosLobos](https://discourse.slimframework.com/u/LosLobos)
#### Post date: [March 28, 2017, 7:28am UTC](https://discourse.slimframework.com/t/getting-request-attributes-in-error-handler/1332/3 "2017-03-28T07:28:08Z")

</div>

This could be an approuch but the problem is that I dealing with multiple type of Exceptions. And if I filter the **$exception**  **using instanceof** I’ll be excluding the other Exceptions and I’ll not be catching all the Errors. ( Exceptions, ArgumentInvalidExceptions, other type of exceptions… )

I think using the Container is the best idea at the moment when dealing with this situation.

---

<div class="post-metadata">

### Author: ![Tsaukpaetra](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/tsaukpaetra/32/964_2.png) [@Tsaukpaetra](https://discourse.slimframework.com/u/Tsaukpaetra)
#### Post date: [February 21, 2022, 7:12pm UTC](https://discourse.slimframework.com/t/getting-request-attributes-in-error-handler/1332/4 "2022-02-21T19:12:42Z")

</div>

Resurrecting this because I was having this exact issue.

It turns out, the _order_ in which you add your middlewares is very important! Namely, if you have a middleware that’s adding attributes and things, and you want it available in the error middleware, you need to add it _after_ the error middleware in the chain.

This seems kinda contrary to the template example, but I noticed that as soon as I did that the attributes were available in the error handler’s $Request object.

I’m entirely convinced I’m doing something wrong, but in v4 it seems to work this way. 🤷

---

<div class="post-metadata">

### Author: ![dhorrigan](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/dhorrigan/32/1433_2.png) [@dhorrigan](https://discourse.slimframework.com/u/dhorrigan)
#### Post date: [February 22, 2022, 3:15am UTC](https://discourse.slimframework.com/t/getting-request-attributes-in-error-handler/1332/5 "2022-02-22T03:15:50Z")

</div>

Slim middleware is a stack, which means it is processed last in, first out (LIFO). So, yes, order definitely matters. However, the Error Handler should almost always be the last middleware added. This is so the error handler is setup as early as possible before your app code is ran. By putting your middleware before it in the stack, an error in your middleware will not get get handled by the Error Handler, causing unexpected behavior.

**Our Solution**

Create a `CurrentUser` object to store the currently logged in user. Inject this into the Error Handler and Auth middleware. You set the current user in your middleware like `$this->currentUser->set($user)`, and in the Error Handler, something like `$userData = $currentUser->get() ?? 'unknown'`.

This works because the container injects the same shared `CurrentUser` object to both.

Side note: If you are using Monolog and want the user info in the log context, you can add a Processor that injects it on every log, not just errors. You can take it a step further and only add that processor after the user is loaded. This way if an error happens during auth, it won’t try to add an empty user to the log.

Hope that helps
