elsa-workflows/elsa-core · error · ArgumentException

Invalid target type.

Error message

Invalid target type.

What it means

ConvertAnyDateType throws ArgumentException "Invalid target type." when the requested target type is not one of DateTime, DateTimeOffset, or DateOnly. The date-conversion helper only supports those three targets by design.

Solutions

  1. Target only DateTime, DateTimeOffset, or DateOnly and post-process (e.g. convert DateTime to TimeOnly via new TimeOnly(dt.Hour, dt.Minute))
  2. Parse TimeOnly strings directly with TimeOnly.Parse before/instead of ConvertTo
  3. Add a custom type converter for the target type if it must be supported
  4. Catch ArgumentException for unsupported target types and handle it as a configuration error

Example fix

// before
var t = (TimeOnly)ObjectConverter.ConvertTo("08:30", typeof(TimeOnly));
// after
var t = TimeOnly.Parse("08:30", CultureInfo.InvariantCulture);
Defensive patterns

Strategy: validation

Validate before calling

bool isSupportedDateTarget(Type t) => t == typeof(DateTime) || t == typeof(DateTimeOffset) || t == typeof(DateOnly);

Try / catch

try { result = ObjectConverter.ConvertTo(v, targetType); }
catch (ArgumentException ex) when (ex.Message == "Invalid target type.") { throw new NotSupportedException($"{targetType} is not a supported date target"); }

Prevention

When it happens

Trigger: Calling ConvertTo (reaching ConvertAnyDateType) with a date-like intent but an unsupported target type, e.g. TimeOnly, TimeSpan, or a custom date wrapper type.

Common situations: Attempting to convert to TimeOnly or custom calendar types expecting ObjectConverter to support them.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of elsa-workflows/elsa-core@fe9217bdfa (2026-09-13). Data as JSON: /api/errors/32887816f6149536. Report an issue: GitHub.

Appendix: source

Thrown at src/modules/Elsa.Expressions/Helpers/ObjectConverter.cs:374

                DateTimeOffset dateTimeOffset => dateTimeOffset.DateTime,
                DateOnly date => new(date.Year, date.Month, date.Day),
                _ => throw new ArgumentException("Invalid value type.")
            },
            { } t when t == typeof(DateTimeOffset) => value switch
            {
                DateTime dateTime => new(dateTime),
                DateTimeOffset dateTimeOffset => dateTimeOffset,
                DateOnly date => new(date.Year, date.Month, date.Day, 0, 0, 0, TimeSpan.Zero),
                _ => throw new ArgumentException("Invalid value type.")
            },
            { } t when t == typeof(DateOnly) => value switch
            {
                DateTime dateTime => new(dateTime.Year, dateTime.Month, dateTime.Day),
                DateTimeOffset dateTimeOffset => new(dateTimeOffset.Year, dateTimeOffset.Month, dateTimeOffset.Day),
                DateOnly date => date,
                _ => throw new ArgumentException("Invalid value type.")
            },
            _ => throw new ArgumentException("Invalid target type.")
        };
    }
}

View on GitHub (pinned to fe9217bdfa)