tursodatabase/turso · error · ArgumentException

Invalid value {origin} for enum type {SeekOrigin}.

Error message

Invalid value {origin} for enum type {SeekOrigin}.

What it means

SqliteBlob.Seek resolves the target position with an exhaustive switch over SeekOrigin; any value outside Begin/Current/End falls into the discard arm and throws ArgumentException. SeekOrigin is a plain enum, so an out-of-range integer can be cast to it; the guard rejects such values instead of silently computing an undefined position.

Source

Thrown at bindings/dotnet/src/Turso.Data.Sqlite/SqliteBlob.cs:77

    {
        Persist();
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        ValidateBuffer(buffer, offset, count);
        return GetStream().Read(buffer, offset, count);
    }

    public override long Seek(long offset, SeekOrigin origin)
    {
        var stream = GetStream();
        var position = origin switch
        {
            SeekOrigin.Begin => offset,
            SeekOrigin.Current => stream.Position + offset,
            SeekOrigin.End => stream.Length + offset,
            _ => throw new ArgumentException(Properties.Resources.InvalidEnumValue(typeof(SeekOrigin), origin), nameof(origin))
        };
        if (position < 0)
            throw new IOException(Properties.Resources.SeekBeforeBegin);

        stream.Position = position;
        return position;
    }

    public override void SetLength(long value)
    {
        throw new NotSupportedException(Properties.Resources.ResizeNotSupported);
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        if (_readOnly)
            throw new NotSupportedException(Properties.Resources.WriteNotSupported);

View on GitHub (pinned to 244cde92a7)

Solutions

  1. Validate at your boundary before calling: if (!Enum.IsDefined(typeof(SeekOrigin), origin)) reject the input.
  2. Map numeric codes to SeekOrigin explicitly (switch on the int) instead of blind-casting.
  3. Use Enum.TryParse<SeekOrigin>() when the value arrives as text.

Example fix

// before
var origin = (SeekOrigin)codeFromWire;   // codeFromWire == 7
blob.Seek(0, origin);                    // throws: Invalid value 7 for enum type SeekOrigin

// after
if (!Enum.IsDefined(typeof(SeekOrigin), (SeekOrigin)codeFromWire))
    throw new ArgumentOutOfRangeException(nameof(codeFromWire));
blob.Seek(0, (SeekOrigin)codeFromWire);
Defensive patterns

Strategy: validation

Validate before calling

static bool IsValidSeekOrigin(int raw) => raw is >= 0 and <= 2; // Begin, Current, End

if (IsValidSeekOrigin(codeFromWire))
    blob.Seek(offset, (SeekOrigin)codeFromWire);

Type guard

static SeekOrigin? ToSeekOrigin(int raw) => raw switch {
    0 => SeekOrigin.Begin,
    1 => SeekOrigin.Current,
    2 => SeekOrigin.End,
    _ => null, // caller rejects null
};

Prevention

When it happens

Trigger: Casting a raw int/byte (protocol tag, serialized value) to SeekOrigin without validation, e.g. (SeekOrigin)99; arithmetic on enum values that leaves the defined range; forwarding a user- or config-supplied origin into Seek.

Common situations: Binary protocol parsers that encode seek-origin as a numeric code; ports from languages with open enums; fuzz/randomized unit tests feeding arbitrary origins; data-driven stream wrappers forwarding external input.

Understand the failure class

Background: "invalid argument", "unknown mode", "not supported": invalid enum-like argument errors explained — this error's family across 19 libraries.

Related errors


AI-assisted analysis of tursodatabase/turso@244cde92a7 (2026-08-20). Data as JSON: /api/errors/ce13c1570541cd74. Report an issue: GitHub.