Skip to content

Commit

Permalink
Added error handling and empty array check (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
zulander1 authored Dec 12, 2022
1 parent 812d633 commit becd81e
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 179 deletions.
24 changes: 19 additions & 5 deletions src/Redis.OM/RedisConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,29 @@ internal RedisConnection(IDatabase db)
/// <inheritdoc/>
public RedisReply Execute(string command, params string[] args)
{
var result = _db.Execute(command, args);
return new RedisReply(result);
try
{
var result = _db.Execute(command, args);
return new RedisReply(result);
}
catch (Exception ex)
{
throw new Exception($"{ex.Message}{Environment.NewLine}Failed on {command} {string.Join(" ", args)}", ex);
}
}

/// <inheritdoc/>
public async Task<RedisReply> ExecuteAsync(string command, params string[] args)
{
var result = await _db.ExecuteAsync(command, args);
return new RedisReply(result);
try
{
var result = await _db.ExecuteAsync(command, args);
return new RedisReply(result);
}
catch (Exception ex)
{
throw new Exception($"{ex.Message}{Environment.NewLine}Failed on {command} {string.Join(" ", args)}", ex);
}
}

/// <inheritdoc/>
Expand Down Expand Up @@ -72,4 +86,4 @@ public void Dispose()
{
}
}
}
}
16 changes: 10 additions & 6 deletions src/Redis.OM/Searching/SearchResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public IDictionary<string, T> DocumentsAs<T>()
/// </summary>
/// <typeparam name="T">The type.</typeparam>
#pragma warning disable SA1402

public class SearchResponse<T>
#pragma warning restore SA1402
where T : notnull
Expand Down Expand Up @@ -101,13 +102,16 @@ public SearchResponse(RedisReply val)
var docId = (string)vals[i];
var documentHash = new Dictionary<string, string>();
var docArray = vals[i + 1].ToArray();
for (var j = 0; j < docArray.Length; j += 2)
if (docArray.Length > 1)
{
documentHash.Add(docArray[j], docArray[j + 1]);
}
for (var j = 0; j < docArray.Length; j += 2)
{
documentHash.Add(docArray[j], docArray[j + 1]);
}

var obj = RedisObjectHandler.FromHashSet<T>(documentHash);
Documents.Add(docId, obj);
var obj = RedisObjectHandler.FromHashSet<T>(documentHash);
Documents.Add(docId, obj);
}
}
}
}
Expand Down Expand Up @@ -155,4 +159,4 @@ private static SearchResponse<T> PrimitiveSearchResponse(RedisReply redisReply)
return response;
}
}
}
}
20 changes: 9 additions & 11 deletions test/Redis.OM.Unit.Tests/CoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,17 +327,15 @@ public async Task SimpleJsonInsertWhenAsync()
var host = Environment.GetEnvironmentVariable("STANDALONE_HOST_PORT") ?? "localhost";
var provider = new RedisConnectionProvider($"redis://{host}");
var connection = provider.Connection;
var collection = new RedisCollection<Person>(provider.Connection);
var collection = new RedisCollection<BasicJsonObject>(provider.Connection);

var obj = new Person { Name = "Steve", Age = 33 };
var obj = new BasicJsonObject { Name = "Steve" };
var key = await collection.InsertAsync(obj, WhenKey.NotExists);
Assert.NotNull(key);
var reconstituted = await collection.FindByIdAsync(key);
Assert.NotNull(reconstituted);
Assert.Equal("Steve", reconstituted.Name);
Assert.Equal(33, reconstituted.Age);
obj.Name = "Shachar";
obj.Age = null;

var res = await collection.InsertAsync(obj, WhenKey.NotExists); // this should fail
Assert.Null(res);
Expand All @@ -346,14 +344,17 @@ public async Task SimpleJsonInsertWhenAsync()
Assert.Equal(key,res);
reconstituted = await collection.FindByIdAsync(key);
Assert.NotNull(reconstituted);
Assert.Null(reconstituted.Age);
Assert.Equal("Shachar" , reconstituted.Name);

await connection.UnlinkAsync(key);
await collection.InsertAsync(obj, WhenKey.NotExists, TimeSpan.FromMilliseconds(5000));
var k2 = await collection.InsertAsync(obj, WhenKey.NotExists, TimeSpan.FromMilliseconds(5000));
Assert.NotNull(k2);
Assert.Equal(key, k2);
var expiration = (long)await connection.ExecuteAsync("PTTL", key);
Assert.Equal(key.Split(":")[1], obj.Id);
Assert.True(expiration>4000);
await Task.Delay(1000);
Assert.True(connection.Execute("EXISTS", key) == 1, $"Expected: {key} to exist, it did not.");
res = await collection.InsertAsync(obj, WhenKey.NotExists, TimeSpan.FromMilliseconds(5000));
Assert.Null(res);
expiration = (long)await connection.ExecuteAsync("PTTL", key);
Expand All @@ -380,17 +381,15 @@ public void SimpleJsonInsertWhen()
var host = Environment.GetEnvironmentVariable("STANDALONE_HOST_PORT") ?? "localhost";
var provider = new RedisConnectionProvider($"redis://{host}");
var connection = provider.Connection;
var collection = new RedisCollection<Person>(provider.Connection);
var collection = new RedisCollection<BasicJsonObject>(provider.Connection);

var obj = new Person { Name = "Steve", Age = 33 };
var obj = new BasicJsonObject { Name = "Steve" };
var key = collection.Insert(obj, WhenKey.NotExists);
Assert.NotNull(key);
var reconstituted = collection.FindById(key);
Assert.NotNull(reconstituted);
Assert.Equal("Steve", reconstituted.Name);
Assert.Equal(33, reconstituted.Age);
obj.Name = "Shachar";
obj.Age = null;

var res = collection.Insert(obj, WhenKey.NotExists); // this should fail
Assert.Null(res);
Expand All @@ -399,7 +398,6 @@ public void SimpleJsonInsertWhen()
Assert.Equal(key,res);
reconstituted = collection.FindById(key);
Assert.NotNull(reconstituted);
Assert.Null(reconstituted.Age);
Assert.Equal("Shachar" , reconstituted.Name);

connection.Unlink(key);
Expand Down
Loading

0 comments on commit becd81e

Please sign in to comment.