protected function getContext(\Exception $exception = null)
    {
        if ($exception) {
            $this->saveException($exception);
        }

        $loggedOnUser = (bool) $this->users->getCurrentUser() ?: false;
        $showLoggedOff = (bool) $this->config->get('general/debug_show_loggedoff', false);

        // Note: We set this to a high value deliberately. If 'config' is not yet available, the
        // user can't influence this, so it shouldn't be too low.
        $traceLimit = (int) $this->config->get('general/debug_trace_argument_limit', 40);

        // Grab a section of the file that threw the exception, so we can show it.
        $filePath = $exception ? $exception->getFile() : null;
        $lineNumber = $exception ? $exception->getLine() : null;

        if ($filePath && $lineNumber) {
            $phpFile = file($filePath) ?: [];
            $snippet = implode('', array_slice($phpFile, max(0, $lineNumber - 6), 11));
        } else {
            $snippet = false;
        }

        // We might or might not have $this->app['request'] yet, which is used in the
        // template to show the request variables. Use it, or grab what we can get.
        $request = $this->requestStack->getCurrentRequest() ?: Request::createFromGlobals();

        return [
            'debug'        => ($this->debug && ($loggedOnUser || $showLoggedOff)),
            'request'      => $request,
            'request_data' => RequestSanitiser::filter($request),
            'exception'    => [
                'object'      => $exception,
                'class_name'  => $exception ? (new \ReflectionClass($exception))->getShortName() : null,
                'class_fqn'   => $exception ? get_class($exception) : null,
                'file_path'   => $filePath,
                'file_name'   => basename($filePath),
                'trace'       => $exception ? $this->getSafeTrace($exception) : null,
                'snippet'     => $snippet,
                'trace_limit' => $traceLimit,
            ],
        ];
    }