Skip to content

Commit

Permalink
Handle exceptions in variable loading and fix type checking.
Browse files Browse the repository at this point in the history
Added try-catch block to log failures when reading variables from storage, ensuring robust error handling. Also refined type checking for `ExpandoObject` deserialization in `ObjectConverter` to prevent invalid operations.
  • Loading branch information
sfmskywalker committed Jan 1, 2025
1 parent adf2a67 commit fdd71f8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/modules/Elsa.Expressions/Helpers/ObjectConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static Result TryConvertTo(this object? value, Type targetType, ObjectCon
return underlyingTargetType switch
{
{ } t when t == typeof(string) => jsonNode.ToString(),
{ } t when t == typeof(ExpandoObject) => JsonSerializer.Deserialize<ExpandoObject>(jsonNode.ToJsonString()),
{ } t when t == typeof(ExpandoObject) && jsonNode.GetValueKind() == JsonValueKind.Object => JsonSerializer.Deserialize<ExpandoObject>(jsonNode.ToJsonString()),
{ } t when t != typeof(object) || converterOptions?.DeserializeJsonObjectToObject == true => jsonNode.Deserialize(targetType, serializerOptions),
_ => jsonNode
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,26 @@ public async Task LoadVariablesAsync(WorkflowExecutionContext workflowExecutionC
continue;

var id = GetStateId(variable);
var value = await driver.ReadAsync(id, storageDriverContext);
if (value == null) continue;

register.Declare(variable);
try
{
var value = await driver.ReadAsync(id, storageDriverContext);
if (value == null) continue;

register.Declare(variable);

if (!variable.TryParseValue(value, out var parsedValue))
{
logger.LogWarning("Failed to parse value for variable {VariableId} of type {VariableType} with value {Value}", variable.Id, variable.GetVariableType().FullName, value);
continue;
}

if (!variable.TryParseValue(value, out var parsedValue))
variable.Set(register, parsedValue);
}
catch (Exception e)
{
logger.LogWarning("Failed to parse value for variable {VariableId} of type {VariableType} with value {Value}", variable.Id, variable.GetVariableType().FullName, value);
continue;
logger.LogError(e, "Failed to read variable {VariableId} from storage driver {StorageDriverType}", variable.Id, driver.GetType().FullName);
}

variable.Set(register, parsedValue);
}
}
}
Expand Down

0 comments on commit fdd71f8

Please sign in to comment.