# How to handle 400 errors

**URL:** <https://discourse.slimframework.com/t/how-to-handle-400-errors/1041>\
**Category:** Questions\
**Created:** [December 28, 2016, 2:59pm UTC](https://discourse.slimframework.com/t/how-to-handle-400-errors/1041 "2016-12-28T14:59:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![bparz](https://avatars.discourse-cdn.com/v4/letter/b/e495f1/32.png) [@bparz](https://discourse.slimframework.com/u/bparz)\
**Post date:** [December 28, 2016, 2:59pm UTC](https://discourse.slimframework.com/t/how-to-handle-400-errors/1041/1 "2016-12-28T14:59:13Z")

</div>

Hi -  
I’m migrating my API from Slim 2 to Slim 3 and trying to get a hold of some of the new concepts, including how to handle dependencies/middleware, etc.

In Slim 2, if the user’s API request could not be met (because of internal API logic), I would send the request to an external function, ie:

$app-\>get(’/’, function() use($app){  
$result = $from-\>externalClass();  
if(conditions met)  
{  
}else{  
handle\_error($result[“error”]);  
}

});

```
function handle_error($error,$user_request="")
{
  global $app;
  if($user_request)
  {
    //logging
  }
 $app->halt(400,json_encode($error));
}

```

However in Slim 3, I do not have access to the response (nor halt). I could simply write in the route:  
$response-\>withJSON($result[“error”],400):

Pretty - but I’d love to abstract these. Would someone be kind enough to point me in the right direction?  
How do I access $response outside of the route?  
Thanks in advance.
