Skip to content

Commit acdc9e0

Browse files
committed
Fix argument validation, error handling, and exception correctness
- Replace manual argument throws with ThrowIf helpers (ThrowIfNull, ThrowIfNullOrEmpty) in ElasticReadOnlyRepositoryBase - Fix VersionedIndex alias error using wrong response variable, which reported the wrong error message and exception on alias fetch failures - Fix incomplete error message in AliasExistsAsync (missing 'exists') - Log error when RefreshForConsistency fails instead of silently swallowing Made-with: Cursor
1 parent c0d1fa4 commit acdc9e0

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

src/Foundatio.Repositories.Elasticsearch/Configuration/VersionedIndex.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.Linq;
@@ -105,7 +105,7 @@ protected async Task<bool> AliasExistsAsync(string alias)
105105
if (response.ApiCall.Success)
106106
return response.Exists;
107107

108-
throw new RepositoryException(response.GetErrorMessage($"Error checking to see if alias {alias}"), response.OriginalException);
108+
throw new RepositoryException(response.GetErrorMessage($"Error checking to see if alias {alias} exists"), response.OriginalException);
109109
}
110110

111111
public override async Task DeleteAsync()
@@ -257,7 +257,7 @@ protected virtual async Task<IList<IndexInfo>> GetIndexesAsync(int version = -1)
257257
_logger.LogRequest(aliasResponse);
258258

259259
if (!aliasResponse.IsValid)
260-
throw new RepositoryException(response.GetErrorMessage($"Error getting index aliases for {filter}"), response.OriginalException);
260+
throw new RepositoryException(aliasResponse.GetErrorMessage($"Error getting index aliases for {filter}"), aliasResponse.OriginalException);
261261

262262
var indices = response.Records
263263
.Where(i => version < 0 || GetIndexVersion(i.Index) == version)

src/Foundatio.Repositories.Elasticsearch/Repositories/ElasticReadOnlyRepositoryBase.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,7 @@ public Task<FindResults<T>> FindAsync(IRepositoryQuery query, ICommandOptions op
357357
if (options.HasAsyncQueryId())
358358
{
359359
var queryId = options.GetAsyncQueryId();
360-
if (String.IsNullOrEmpty(queryId))
361-
throw new ArgumentNullException("AsyncQueryId must not be null");
360+
ArgumentException.ThrowIfNullOrEmpty(queryId, nameof(queryId));
362361

363362
var response = await _client.AsyncSearch.GetAsync<TResult>(queryId, s =>
364363
{
@@ -437,8 +436,7 @@ public async Task RemoveQueryAsync(string queryId)
437436

438437
private async Task<FindResults<TResult>> GetNextPageFunc<TResult>(FindResults<TResult> previousResults, IRepositoryQuery query, ICommandOptions options) where TResult : class, new()
439438
{
440-
if (previousResults == null)
441-
throw new ArgumentException(nameof(previousResults));
439+
ArgumentNullException.ThrowIfNull(previousResults);
442440

443441
string scrollId = previousResults.GetScrollId();
444442
if (!String.IsNullOrEmpty(scrollId))
@@ -536,8 +534,7 @@ public virtual async Task<CountResult> CountAsync(IRepositoryQuery query, IComma
536534
if (options.HasAsyncQueryId())
537535
{
538536
var queryId = options.GetAsyncQueryId();
539-
if (String.IsNullOrEmpty(queryId))
540-
throw new ArgumentNullException("AsyncQueryId must not be null");
537+
ArgumentException.ThrowIfNullOrEmpty(queryId, nameof(queryId));
541538

542539
var response = await _client.AsyncSearch.GetAsync<T>(queryId, s =>
543540
{
@@ -835,12 +832,14 @@ protected bool ShouldReturnDocument(T document, ICommandOptions options)
835832

836833
protected async Task RefreshForConsistency(IRepositoryQuery query, ICommandOptions options)
837834
{
838-
// if not using eventual consistency, force a refresh
839835
if (options.GetConsistency(DefaultConsistency) != Consistency.Eventual)
840836
{
841837
string[] indices = ElasticIndex.GetIndexesByQuery(query);
842838
var response = await _client.Indices.RefreshAsync(indices);
843-
_logger.LogRequest(response);
839+
if (response.IsValid)
840+
_logger.LogRequest(response);
841+
else
842+
_logger.LogErrorRequest(response, "Failed to refresh indices for immediate consistency");
844843
}
845844
}
846845

0 commit comments

Comments
 (0)