babalae/better-genshin-impact · warning · InvalidDataException

下载的图片内容为空。

Error message

下载的图片内容为空。

What it means

An InvalidDataException thrown by DownloadAndSaveAsync after the stream loop completes if totalBytes is zero. It rejects an empty body: the request succeeded (EnsureSuccessStatusCode passed) but no image bytes were received, so the file would be invalid/empty. This runs before ValidateImage so it gives a clearer message than an image-decode failure.

Source

Thrown at BetterGenshinImpact/Service/BannerImageService.cs:129

                int read;
                while ((read = await responseStream
                           .ReadAsync(buffer.AsMemory(), linkedCancellationToken)
                           .ConfigureAwait(false)) > 0)
                {
                    totalBytes += read;
                    if (totalBytes > MaxDownloadBytes)
                    {
                        throw new InvalidDataException($"图片大小超过 {MaxDownloadBytes / 1024 / 1024} MB 限制。");
                    }

                    await fileStream
                        .WriteAsync(buffer.AsMemory(0, read), linkedCancellationToken)
                        .ConfigureAwait(false);
                }

                if (totalBytes == 0)
                {
                    throw new InvalidDataException("下载的图片内容为空。");
                }
            }

            ValidateImage(tempPath);
            linkedCancellationToken.ThrowIfCancellationRequested();

            lock (_fileCommitLock)
            {
                if (operationId != Volatile.Read(ref _latestOperationId))
                {
                    return false;
                }

                linkedCancellationToken.ThrowIfCancellationRequested();
                // 保存图片到本地
                File.Move(tempPath, NetworkImagePath, true);
            }

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Verify the URL points to a real, accessible image (open it in a browser).
  2. If the host requires auth/headers, configure the HttpClient accordingly or use a direct link.
  3. Retry once in case of a transient empty response from the CDN.
  4. Catch InvalidDataException and prompt the user to supply a valid image URL.

Example fix

null
Defensive patterns

Strategy: try-catch

Validate before calling

// No reliable pre-check for an empty 200 body; verify the URL resolves to a real image.
// Optionally HEAD the URL and reject Content-Length == 0 when the host reports it.

Type guard

null

Try / catch

try { var ok = await _banner.DownloadAndSaveAsync(url, ct); }
catch (InvalidDataException ex) { Toast.Warning(ex.Message); }

Prevention

When it happens

Trigger: The server returns HTTP 200 with an empty body (Content-Length 0 or a chunked stream that yields no data). The URL technically resolved and responded, but delivered zero bytes.

Common situations: A broken/expired image link that returns 200 with no body; a host requiring auth/cookies that silently returns empty; a CDN edge returning an empty object; a misconfigured URL pointing at a directory or non-image resource that returns nothing.

Related errors


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/b2c9709a430489a8. Report an issue: GitHub.