elsa-workflows/elsa-core · error · TypeConversionException

Failed to convert an object of type

Error message

Failed to convert an object of type {sourceType} to {underlyingTargetType}

What it means

ReturnOrThrow in ObjectConverter.ConvertTo is the generic failure path: when any conversion attempt fails, non-strict mode returns the target type's default value for backward compatibility, but strict mode throws TypeConversionException stating the source type could not be converted to the target type.

Solutions

  1. Fix the source value type so it matches the target (e.g. pass "42" for int targets, not "abc")
  2. Add explicit parsing/validation before conversion (int.TryParse, DateTime.TryParse, etc.)
  3. Disable strict conversion mode if the legacy default-value behavior is acceptable
  4. Inspect the inner exception 'e' captured in the TypeConversionException for the exact failing conversion step

Example fix

// before
var count = (int)ObjectConverter.ConvertTo(input, typeof(int)); // input may be "abc"
// after
if (!int.TryParse(input?.ToString(), out var count))
    throw new FormatException("Input must be a numeric string");
Defensive patterns

Strategy: try-catch

Validate before calling

// pre-validate numeric conversion
if (targetType == typeof(int) && !int.TryParse(source?.ToString(), out _)) throw new FormatException("Not numeric");

Type guard

bool canConvert<T>(object? v) => v is T || (v is string s && typeof(T) != typeof(string) && TryParseable<T>(s));

Try / catch

try { result = ObjectConverter.ConvertTo(value, targetType); }
catch (TypeConversionException ex)
{ logger.LogError(ex.InnerException, "Conversion {Source} -> {Target} failed", sourceType, targetType); result = targetType.GetDefaultValue(); }

Prevention

When it happens

Trigger: Calling ConvertTo (with strictMode on) where the value's type cannot be converted to the underlying target type through any of the converter's supported paths — e.g. converting a non-numeric string to int, or an object to a struct with no conversion.

Common situations: Workflow input/variable binding where a user-supplied value has the wrong type; enabling StrictExpressions/strict mode in an environment that previously silently produced defaults.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

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

        try
        {
            return Convert.ChangeType(value, underlyingTargetType, CultureInfo.InvariantCulture);
        }
        catch (FormatException e)
        {
            return ReturnOrThrow(e);
        }
        catch (InvalidCastException e)
        {
            return ReturnOrThrow(e);
        }

        object? ReturnOrThrow(Exception e)
        {
            if (!strictMode)
                return targetType.GetDefaultValue(); // Backward compatibility: return default value if strict mode is off.

            throw new TypeConversionException($"Failed to convert an object of type {sourceType} to {underlyingTargetType}", value, underlyingTargetType, e);
        }
    }

    /// <summary>
    /// Returns true if the specified type is a date-like type, false otherwise.
    /// </summary>
    private static bool IsDateType(Type type)
    {
        var dateTypes = new[]
        {
            typeof(DateTime), typeof(DateTimeOffset), typeof(DateOnly)
        };

        return dateTypes.Contains(type);
    }

    /// <summary>
    /// Converts any date type to the specified target type.

View on GitHub (pinned to fe9217bdfa)