How to handle 400 errors

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.