protected function error($err)
    {
        $type = get_class($err);
        $msg = $err->getMessage();

        try {
            if (!$this->error_callbacks->isEmpty()) {
                foreach ($this->error_callbacks as $callback) {
                    if (is_callable($callback)) {
                        if (is_string($callback)) {
                            $callback($this, $msg, $type, $err);

                            return;
                        } else {
                            call_user_func($callback, $this, $msg, $type, $err);

                            return;
                        }
                    } else {
                        if (null !== $this->service && null !== $this->response) {
                            $this->service->flash($err);
                            $this->response->redirect($callback);
                        }
                    }
                }
            } else {
                $this->response->code(500);

                while (ob_get_level() >= $this->output_buffer_level) {
                    ob_end_clean();
                }

                throw new UnhandledException($msg, $err->getCode(), $err);
            }
        } catch (Throwable $e) { // PHP 7 compatibility
            // Make sure to clean the output buffer before bailing
            while (ob_get_level() >= $this->output_buffer_level) {
                ob_end_clean();
            }

            throw $e;
        } catch (Exception $e) { // TODO: Remove this catch block once PHP 5.x support is no longer necessary.
            // Make sure to clean the output buffer before bailing
            while (ob_get_level() >= $this->output_buffer_level) {
                ob_end_clean();
            }

            throw $e;
        }

        // Lock our response, since we probably don't want
        // anything else messing with our error code/body
        $this->response->lock();
    }