# Handle InvalidArgumentException thrown by RequestFactory

**URL:** <https://discourse.slimframework.com/t/handle-invalidargumentexception-thrown-by-requestfactory/3677>\
**Category:** Questions\
**Created:** [November 27, 2019, 10:40am UTC](https://discourse.slimframework.com/t/handle-invalidargumentexception-thrown-by-requestfactory/3677 "2019-11-27T10:40:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dennis](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/dennis/32/1031_2.png) [@Dennis](https://discourse.slimframework.com/u/Dennis)\
**Post date:** [November 27, 2019, 10:40am UTC](https://discourse.slimframework.com/t/handle-invalidargumentexception-thrown-by-requestfactory/3677/1 "2019-11-27T10:40:55Z")

</div>

Hi All,

I have the following problem. If I create a request with invalid headers, my application will die before entering errormiddleware.

Such a request van be:  
GET [url]  
0: 0

Where “0: 0” is the first header line.

In the $app-\>run() method, we’ll see that first the request is created and then the request will be handled. So the question is: What is the best way to handle such a user error which happens before entering Error Middleware?

---

<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:** [November 27, 2019, 2:29pm UTC](https://discourse.slimframework.com/t/handle-invalidargumentexception-thrown-by-requestfactory/3677/2 "2019-11-27T14:29:56Z")

</div>

This happens because the HTTP request header name must be an [RFC 7230](https://tools.ietf.org/html/rfc7230#section-3.2.4) compatible string.

The name “0” is not a valid request header name.

Instead of calling `$app->run()` you could pass you own “cleaned” ServerRequest object.

```php
$app->run(MyRequestFactory::create());

```

Another approach would be to implement your own ServerRequest factory and “overwrite” the container `ServerRequestFactoryInterface` definiton.

```php
$app = AppFactory::createFromContainer($container);

```

---

<div class="post-metadata">

**Author:** ![Dennis](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/dennis/32/1031_2.png) [@Dennis](https://discourse.slimframework.com/u/Dennis)\
**Post date:** [November 28, 2019, 7:57am UTC](https://discourse.slimframework.com/t/handle-invalidargumentexception-thrown-by-requestfactory/3677/3 "2019-11-28T07:57:28Z")

</div>

Hi odan,

Thank you for taking the time to reply.

I am aware that I could filter out all invalid lines from the request header.

The problem is that we are not in control of what request the user sends. It could be all kinds of invalid.

The user should get bad request error, explaining what went wrong (in my opinion).  
Your answer implies to filter out the bad parts and create a request with the remaining good parts.

The problem is also that in Slim Framework, the Error handler only works after the request object is created succesfully.

---

<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:** [November 28, 2019, 8:33am UTC](https://discourse.slimframework.com/t/handle-invalidargumentexception-thrown-by-requestfactory/3677/4 "2019-11-28T08:33:55Z")

</div>

As a workaround you could try to catch all exceptions from the `run()` method.

```php
<?php

// bootstrap code ...

try {
    $app->run();
} catch (Throwable $exception) {
    http_response_code(400);
    echo sprintf('Bad Request: %s', $exception->getMessage());
}

```

Result: `Bad Request: Header name must be an RFC 7230 compatible string.`

---

<div class="post-metadata">

**Author:** ![Dennis](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/dennis/32/1031_2.png) [@Dennis](https://discourse.slimframework.com/u/Dennis)\
**Post date:** [November 28, 2019, 1:16pm UTC](https://discourse.slimframework.com/t/handle-invalidargumentexception-thrown-by-requestfactory/3677/5 "2019-11-28T13:16:03Z")

</div>

Hi Odan,

Thanks for your answer. It seems that your solution is the only possible way.

I have a custom error handler with a view template. This means that I must use two different error handlers (my one and also Slim Framework middleware) with the same code.
