public static function createFromException($e, $debug = false, $internalErrorMessage = null)
    {
        Utils::invariant(
            $e instanceof Exception || $e instanceof Throwable,
            'Expected exception, got %s',
            Utils::getVariableType($e)
        );

        $internalErrorMessage = $internalErrorMessage ?: self::$internalErrorMessage;

        if ($e instanceof ClientAware) {
            $formattedError = [
                'message'  => $e->isClientSafe() ? $e->getMessage() : $internalErrorMessage,
                'extensions' => [
                    'category' => $e->getCategory(),
                ],
            ];
        } else {
            $formattedError = [
                'message'  => $internalErrorMessage,
                'extensions' => [
                    'category' => Error::CATEGORY_INTERNAL,
                ],
            ];
        }

        if ($e instanceof Error) {
            $locations = Utils::map(
                $e->getLocations(),
                static function (SourceLocation $loc) {
                    return $loc->toSerializableArray();
                }
            );
            if (! empty($locations)) {
                $formattedError['locations'] = $locations;
            }
            if (! empty($e->path)) {
                $formattedError['path'] = $e->path;
            }
            if (! empty($e->getExtensions())) {
                $formattedError['extensions'] = $e->getExtensions() + $formattedError['extensions'];
            }
        }

        if ($debug) {
            $formattedError = self::addDebugEntries($formattedError, $e, $debug);
        }

        return $formattedError;
    }