protected function handleErrors($request, $response)
    {
        $errorHandler = function () use ($request, $response) {
            //there are alot of arguments
            $args = func_get_args();
            
            //remove the body
            $response->remove('body');
            
            //set the status
            $code = $response->get('code');
            
            if (!$code) {
                $response->set('code', 500);
            }
            
            array_unshift($args, $request, $response);
            
            //parse through middleware
            foreach ($this->errorMiddleware as $callback) {
                //bind callback
                $callback = $callback->bindTo($this, get_class($this));
            
                if (call_user_func_array($callback, $args) === false) {
                    break;
                }
            }
            
            //do we have a body ?
            if ((string) $response->get('body')) {
                $this->output($response);
                exit;
            }
            
            //there maybe another error/exception handler
            //let it be called since we could not do anything
        };
        
        $errorHandler = $errorHandler->bindTo($this, get_class($this));
        
        //global error/exception event
        $this
            ->on('error', $errorHandler, true)
            ->on('exception', $errorHandler, true);
        
        return $this;
    }