iOfficeAI/OfficeCLI · error · ArgumentException

Image source cannot be empty

Error message

Image source cannot be empty

What it means

Thrown by ImageSource.Resolve when the source string is null, empty, or whitespace-only. This is the entry point for all image resolution (file path, data URI, or HTTP URL), and it refuses to proceed with a blank source because the downstream resolvers would produce confusing errors about missing schemes or file-not-found for an empty string.

Source

Thrown at src/officecli/Core/ImageSource.cs:30

/// Resolves image sources from file paths, data URIs, or HTTP(S) URLs into a stream and content type.
/// Supports:
///   - Local file path: "/tmp/logo.png", "C:\images\photo.jpg"
///   - Data URI: "data:image/png;base64,iVBOR..."
///   - HTTP(S) URL: "https://example.com/image.png"
///
/// Returns a content type string compatible with OpenXmlPart.AddImagePart() (e.g. ImagePartType.Png).
/// </summary>
internal static class ImageSource
{
    /// <summary>
    /// Resolve an image source string into a stream and content type string.
    /// Caller is responsible for disposing the returned stream.
    /// The returned contentType can be passed directly to AddImagePart().
    /// </summary>
    public static (Stream Stream, PartTypeInfo ContentType) Resolve(string source)
    {
        if (string.IsNullOrWhiteSpace(source))
            throw new ArgumentException("Image source cannot be empty");

        // Data URI: data:image/png;base64,iVBOR...
        if (source.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
            return ResolveDataUri(source);

        // HTTP(S) URL
        if (source.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
            source.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
            return ResolveUrl(source);

        // Local file path
        return ResolveFile(source);
    }

    /// <summary>
    /// Determine content type string from a file extension (with or without dot).
    /// Returns a value usable with AddImagePart().
    /// </summary>

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Provide a non-empty image source: a local file path, an http(s) URL, or a data URI.
  2. Check that the 'src' (or 'icon'/'preview') property in your Add command dictionary is populated before invoking the handler.
  3. If the source comes from a variable, add a null/empty guard upstream and skip the operation or prompt for a valid source.

Example fix

// before — blank source throws
add image src='' path='/body'

// after — provide a real source
add image src='/tmp/logo.png' path='/body'
Defensive patterns

Strategy: validation

Validate before calling

// Validate source is non-empty before resolving
if (string.IsNullOrWhiteSpace(imageSource))
{
    Console.Error.WriteLine("Image source is required and cannot be empty.");
    return;
}
var (stream, contentType) = ImageSource.Resolve(imageSource);

Try / catch

try
{
    var (stream, contentType) = ImageSource.Resolve(source);
}
catch (ArgumentException ex) when (ex.Message.Contains("cannot be empty"))
{
    // Skip this image or prompt for a valid source
}

Prevention

When it happens

Trigger: Calling ImageSource.Resolve(null), ImageSource.Resolve(""), or ImageSource.Resolve(" "). This happens when an Add image/picture/ole command receives a missing or blank 'src'/'icon'/'preview' property, or when a programmatic caller passes an uninitialized string variable.

Common situations: An agent that constructs an image-add command from a template but forgets to fill in the source field. A batch script where a variable expansion produced an empty string. A data URI that was truncated to just 'data:' or an empty string during serialization.

Related errors


AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13). Data as JSON: /api/errors/ea9aaf1c413c4f53. Report an issue: GitHub.