Skip to content

Commit

Permalink
Merge pull request #46439 from khosbilegt/main
Browse files Browse the repository at this point in the history
Redis Client: Refactored Exception Throwing in Uni Functions
  • Loading branch information
geoand authored Feb 25, 2025
2 parents f4a362a + 61d8865 commit aadb627
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Uni<Response> _bfmadd(K key, V... values) {
nonNull(key, "key");
doesNotContainNull(values, "values");
if (values.length == 0) {
throw new IllegalArgumentException("`values` must contain at least one item");
return Uni.createFrom().failure(new IllegalArgumentException("`values` must contain at least one item"));
}

RedisCommand command = RedisCommand.of(Command.BF_MADD)
Expand All @@ -60,7 +60,7 @@ Uni<Response> _bfmexists(K key, V... values) {
nonNull(key, "key");
doesNotContainNull(values, "values");
if (values.length == 0) {
throw new IllegalArgumentException("`values` must contain at least one item");
return Uni.createFrom().failure(new IllegalArgumentException("`values` must contain at least one item"));
}

RedisCommand command = RedisCommand.of(Command.BF_MEXISTS)
Expand Down Expand Up @@ -91,7 +91,7 @@ Uni<Response> _bfinsert(K key, BfInsertArgs args, V... values) {
nonNull(args, "args");
doesNotContainNull(values, "values");
if (values.length == 0) {
throw new IllegalArgumentException("`values` must contain at least one item");
return Uni.createFrom().failure(new IllegalArgumentException("`values` must contain at least one item"));
}

RedisCommand command = RedisCommand.of(Command.BF_INSERT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Uni<Response> _cmsIncrBy(K key, Map<V, Long> couples) {
nonNull(key, "key");
nonNull(couples, "couples");
if (couples.isEmpty()) {
throw new IllegalArgumentException("`couples` must not be empty");
return Uni.createFrom().failure(new IllegalArgumentException("`couples` must not be empty"));
}
// Create command
RedisCommand cmd = RedisCommand.of(Command.CMS_INCRBY)
Expand Down Expand Up @@ -86,7 +86,7 @@ Uni<Response> _cmsQuery(K key, V... items) {
nonNull(key, "key");
doesNotContainNull(items, "items");
if (items.length == 0) {
throw new IllegalArgumentException("`items` must not be empty");
return Uni.createFrom().failure(new IllegalArgumentException("`items` must not be empty"));
}
// Create command
RedisCommand cmd = RedisCommand.of(Command.CMS_QUERY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Uni<Response> _cfinsert(K key, V... values) {
nonNull(key, "key");
doesNotContainNull(values, "values");
if (values.length == 0) {
throw new IllegalArgumentException("`values` must contain at least one item");
return Uni.createFrom().failure(new IllegalArgumentException("`values` must contain at least one item"));
}

// Create command
Expand All @@ -96,7 +96,7 @@ Uni<Response> _cfinsert(K key, CfInsertArgs args, V... values) {
nonNull(args, "args");
doesNotContainNull(values, "values");
if (values.length == 0) {
throw new IllegalArgumentException("`values` must contain at least one item");
return Uni.createFrom().failure(new IllegalArgumentException("`values` must contain at least one item"));
}
// Create command
RedisCommand cmd = RedisCommand.of(Command.CF_INSERT)
Expand All @@ -114,7 +114,7 @@ Uni<Response> _cfinsertnx(K key, V... values) {
nonNull(key, "key");
doesNotContainNull(values, "values");
if (values.length == 0) {
throw new IllegalArgumentException("`values` must contain at least one item");
return Uni.createFrom().failure(new IllegalArgumentException("`values` must contain at least one item"));
}
// Create command
RedisCommand cmd = RedisCommand.of(Command.CF_INSERTNX)
Expand All @@ -132,7 +132,7 @@ Uni<Response> _cfinsertnx(K key, CfInsertArgs args, V... values) {
doesNotContainNull(values, "values");
nonNull(args, "args");
if (values.length == 0) {
throw new IllegalArgumentException("`values` must contain at least one item");
return Uni.createFrom().failure(new IllegalArgumentException("`values` must contain at least one item"));
}
// Create command
RedisCommand cmd = RedisCommand.of(Command.CF_INSERTNX)
Expand All @@ -150,7 +150,7 @@ Uni<Response> _cfmexists(K key, V... values) {
nonNull(key, "key");
doesNotContainNull(values, "values");
if (values.length == 0) {
throw new IllegalArgumentException("`values` must contain at least one item");
return Uni.createFrom().failure(new IllegalArgumentException("`values` must contain at least one item"));
}
// Create command
RedisCommand cmd = RedisCommand.of(Command.CF_MEXISTS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ final Uni<Response> _hmget(K key, F... fields) {
nonNull(key, "key");
doesNotContainNull(fields, "fields");
if (fields.length == 0) {
throw new IllegalArgumentException("`fields` must not be empty");
return Uni.createFrom().failure(new IllegalArgumentException("`fields` must not be empty"));
}
RedisCommand cmd = RedisCommand.of(Command.HMGET);
cmd.put(marshaller.encode(key));
Expand All @@ -101,7 +101,7 @@ Uni<Response> _hmset(K key, Map<F, V> map) {
nonNull(key, "key");
nonNull(map, "map");
if (map.isEmpty()) {
throw new IllegalArgumentException("`map` must not be empty");
return Uni.createFrom().failure(new IllegalArgumentException("`map` must not be empty"));
}
RedisCommand cmd = RedisCommand.of(Command.HMSET);
cmd.put(marshaller.encode(key));
Expand Down Expand Up @@ -142,7 +142,7 @@ Uni<Response> _hset(K key, Map<F, V> map) {
nonNull(key, "key");
nonNull(map, "map");
if (map.isEmpty()) {
throw new IllegalArgumentException("`map` must not be empty");
return Uni.createFrom().failure(new IllegalArgumentException("`map` must not be empty"));
}
RedisCommand cmd = RedisCommand.of(Command.HSET);
cmd.put(marshaller.encode(key));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ long decodeExpireResponse(K key, Response r) {
Uni<Response> _keys(String pattern) {
nonNull(pattern, "pattern");
if (pattern.isBlank()) {
throw new IllegalArgumentException("`pattern` must not be blank");
return Uni.createFrom().failure(new IllegalArgumentException("`pattern` must not be blank"));
}

return execute(RedisCommand.of(Command.KEYS).put(pattern));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Uni<Response> _sdiff(K... keys) {
notNullOrEmpty(keys, "keys");
doesNotContainNull(keys, "keys");
if (keys.length <= 1) {
throw new IllegalArgumentException("`keys` must contain at least 2 keys");
return Uni.createFrom().failure(new IllegalArgumentException("`keys` must contain at least 2 keys"));
}
return execute(RedisCommand.of(Command.SDIFF).put(marshaller.encode(keys)));
}
Expand All @@ -54,7 +54,7 @@ Uni<Response> _sdiffstore(K destination, K... keys) {
notNullOrEmpty(keys, "keys");
doesNotContainNull(keys, "keys");
if (keys.length <= 1) {
throw new IllegalArgumentException("`keys` must contain at least 2 keys");
return Uni.createFrom().failure(new IllegalArgumentException("`keys` must contain at least 2 keys"));
}
RedisCommand cmd = RedisCommand.of(Command.SDIFFSTORE)
.put(marshaller.encode(destination))
Expand All @@ -66,7 +66,7 @@ Uni<Response> _sinter(K... keys) {
notNullOrEmpty(keys, "keys");
doesNotContainNull(keys, "keys");
if (keys.length <= 1) {
throw new IllegalArgumentException("`keys` must contain at least 2 keys");
return Uni.createFrom().failure(new IllegalArgumentException("`keys` must contain at least 2 keys"));
}
return execute(RedisCommand.of(Command.SINTER).put(marshaller.encode(keys)));
}
Expand All @@ -75,7 +75,7 @@ Uni<Response> _sintercard(K... keys) {
notNullOrEmpty(keys, "keys");
doesNotContainNull(keys, "keys");
if (keys.length <= 1) {
throw new IllegalArgumentException("`keys` must contain at least 2 keys");
return Uni.createFrom().failure(new IllegalArgumentException("`keys` must contain at least 2 keys"));
}

RedisCommand cmd = RedisCommand.of(Command.SINTERCARD).put(keys.length).putAll(marshaller.encode(keys));
Expand All @@ -89,7 +89,7 @@ Uni<Response> _sintercard(int limit, K... keys) {
positive(limit, "limit");

if (keys.length <= 1) {
throw new IllegalArgumentException("`keys` must contain at least 2 keys");
return Uni.createFrom().failure(new IllegalArgumentException("`keys` must contain at least 2 keys"));
}

RedisCommand cmd = RedisCommand.of(Command.SINTERCARD).put(keys.length).putAll(marshaller.encode(keys))
Expand All @@ -101,7 +101,7 @@ Uni<Response> _sinterstore(K destination, K... keys) {
nonNull(destination, "destination");
notNullOrEmpty(keys, "keys");
if (keys.length <= 1) {
throw new IllegalArgumentException("`keys` must contain at least 2 keys");
return Uni.createFrom().failure(new IllegalArgumentException("`keys` must contain at least 2 keys"));
}
RedisCommand cmd = RedisCommand.of(Command.SINTERSTORE)
.put(marshaller.encode(destination))
Expand Down Expand Up @@ -186,7 +186,7 @@ Uni<Response> _sunion(K... keys) {
notNullOrEmpty(keys, "keys");
doesNotContainNull(keys, "keys");
if (keys.length <= 1) {
throw new IllegalArgumentException("`keys` must contain at least 2 keys");
return Uni.createFrom().failure(new IllegalArgumentException("`keys` must contain at least 2 keys"));
}
return execute(RedisCommand.of(Command.SUNION).put(marshaller.encode(keys)));
}
Expand All @@ -196,7 +196,7 @@ Uni<Response> _sunionstore(K destination, K... keys) {
notNullOrEmpty(keys, "keys");
doesNotContainNull(keys, "keys");
if (keys.length <= 1) {
throw new IllegalArgumentException("`keys` must contain at least 2 keys");
return Uni.createFrom().failure(new IllegalArgumentException("`keys` must contain at least 2 keys"));
}
RedisCommand cmd = RedisCommand.of(Command.SUNIONSTORE)
.put(marshaller.encode(destination))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ Uni<Response> _zdiff(K... keys) {
notNullOrEmpty(keys, "keys");
doesNotContainNull(keys, "keys");
if (keys.length < 2) {
throw new IllegalArgumentException("Need at least two keys");
return Uni.createFrom().failure(new IllegalArgumentException("Need at least two keys"));
}
RedisCommand cmd = RedisCommand.of(Command.ZDIFF)
.put(keys.length)
Expand All @@ -142,7 +142,7 @@ Uni<Response> _zdiffWithScores(K... keys) {
notNullOrEmpty(keys, "keys");
doesNotContainNull(keys, "keys");
if (keys.length < 2) {
throw new IllegalArgumentException("Need at least two keys");
return Uni.createFrom().failure(new IllegalArgumentException("Need at least two keys"));
}

RedisCommand cmd = RedisCommand.of(Command.ZDIFF)
Expand All @@ -162,7 +162,7 @@ Uni<Response> _zdiffstore(K destination, K... keys) {
notNullOrEmpty(keys, "keys");
doesNotContainNull(keys, "keys");
if (keys.length < 2) {
throw new IllegalArgumentException("Need at least two keys");
return Uni.createFrom().failure(new IllegalArgumentException("Need at least two keys"));
}
RedisCommand cmd = RedisCommand.of(Command.ZDIFFSTORE)
.put(marshaller.encode(destination))
Expand All @@ -189,7 +189,7 @@ Uni<Response> _zinter(ZAggregateArgs args, K... keys) {
notNullOrEmpty(keys, "keys");
doesNotContainNull(keys, "keys");
if (keys.length < 2) {
throw new IllegalArgumentException("Need at least two keys");
return Uni.createFrom().failure(new IllegalArgumentException("Need at least two keys"));
}
RedisCommand cmd = RedisCommand.of(Command.ZINTER)
.put(keys.length);
Expand All @@ -210,7 +210,7 @@ Uni<Response> _zinterWithScores(ZAggregateArgs arguments, K... keys) {
notNullOrEmpty(keys, "keys");
doesNotContainNull(keys, "keys");
if (keys.length < 2) {
throw new IllegalArgumentException("Need at least two keys");
return Uni.createFrom().failure(new IllegalArgumentException("Need at least two keys"));
}
RedisCommand cmd = RedisCommand.of(Command.ZINTER)
.put(keys.length);
Expand All @@ -231,7 +231,7 @@ Uni<Response> _zintercard(K... keys) {
notNullOrEmpty(keys, "keys");
doesNotContainNull(keys, "keys");
if (keys.length < 2) {
throw new IllegalArgumentException("Need at least two keys");
return Uni.createFrom().failure(new IllegalArgumentException("Need at least two keys"));
}
RedisCommand cmd = RedisCommand.of(Command.ZINTERCARD)
.put(keys.length);
Expand All @@ -247,7 +247,7 @@ Uni<Response> _zintercard(long limit, K... keys) {
notNullOrEmpty(keys, "keys");
doesNotContainNull(keys, "keys");
if (keys.length < 2) {
throw new IllegalArgumentException("Need at least two keys");
return Uni.createFrom().failure(new IllegalArgumentException("Need at least two keys"));
}
positive(limit, "limit");
RedisCommand cmd = RedisCommand.of(Command.ZINTERCARD)
Expand All @@ -267,7 +267,7 @@ Uni<Response> _zinterstore(K destination, ZAggregateArgs arguments, K... keys) {
notNullOrEmpty(keys, "keys");
doesNotContainNull(keys, "keys");
if (keys.length < 2) {
throw new IllegalArgumentException("Need at least two keys");
return Uni.createFrom().failure(new IllegalArgumentException("Need at least two keys"));
}
nonNull(arguments, "arguments");
nonNull(destination, "destination");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ Uni<Response> _tsIncrBy(K key, double value, IncrementArgs args) {
Uni<Response> _tsMAdd(SeriesSample<K>... samples) {
doesNotContainNull(samples, "samples");
if (samples.length == 0) {
throw new IllegalArgumentException("`samples` must not be empty");
return Uni.createFrom().failure(new IllegalArgumentException("`samples` must not be empty"));
}

RedisCommand cmd = RedisCommand.of(Command.TS_MADD);
Expand All @@ -196,7 +196,7 @@ Uni<Response> _tsMGet(MGetArgs args, Filter... filters) {
nonNull(args, "args");
doesNotContainNull(filters, "filters");
if (filters.length == 0) {
throw new IllegalArgumentException("`filters` must not be empty");
return Uni.createFrom().failure(new IllegalArgumentException("`filters` must not be empty"));
}
RedisCommand cmd = RedisCommand.of(Command.TS_MGET).putArgs(args);
cmd.put("FILTER");
Expand All @@ -210,7 +210,7 @@ Uni<Response> _tsMGet(MGetArgs args, Filter... filters) {
Uni<Response> _tsMGet(Filter... filters) {
doesNotContainNull(filters, "filters");
if (filters.length == 0) {
throw new IllegalArgumentException("`filters` must not be empty");
return Uni.createFrom().failure(new IllegalArgumentException("`filters` must not be empty"));
}
RedisCommand cmd = RedisCommand.of(Command.TS_MGET);
cmd.put("FILTER");
Expand All @@ -226,7 +226,7 @@ Uni<Response> _tsMRange(TimeSeriesRange range, Filter... filters) {
nonNull(range, "range");
doesNotContainNull(filters, "filters");
if (filters.length == 0) {
throw new IllegalArgumentException("`filters` must not be empty");
return Uni.createFrom().failure(new IllegalArgumentException("`filters` must not be empty"));
}

RedisCommand cmd = RedisCommand.of(Command.TS_MRANGE).putAll(range.toArgs());
Expand All @@ -244,7 +244,7 @@ Uni<Response> _tsMRange(TimeSeriesRange range, MRangeArgs args, Filter... filter
nonNull(args, "args");
doesNotContainNull(filters, "filters");
if (filters.length == 0) {
throw new IllegalArgumentException("`filters` must not be empty");
return Uni.createFrom().failure(new IllegalArgumentException("`filters` must not be empty"));
}

RedisCommand cmd = RedisCommand.of(Command.TS_MRANGE).putAll(range.toArgs()).putArgs(args);
Expand Down Expand Up @@ -277,7 +277,7 @@ Uni<Response> _tsMRevRange(TimeSeriesRange range, MRangeArgs args, Filter... fil
nonNull(range, "range");
doesNotContainNull(filters, "filters");
if (filters.length == 0) {
throw new IllegalArgumentException("`filters` must not be empty");
return Uni.createFrom().failure(new IllegalArgumentException("`filters` must not be empty"));
}

RedisCommand cmd = RedisCommand.of(Command.TS_MREVRANGE).putAll(range.toArgs()).putArgs(args);
Expand All @@ -293,7 +293,7 @@ Uni<Response> _tsQueryIndex(Filter... filters) {

doesNotContainNull(filters, "filters");
if (filters.length == 0) {
throw new IllegalArgumentException("`filters` must not be empty");
return Uni.createFrom().failure(new IllegalArgumentException("`filters` must not be empty"));
}

RedisCommand cmd = RedisCommand.of(Command.TS_QUERYINDEX);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Uni<Response> _topkAdd(K key, V... items) {
nonNull(key, "key");
doesNotContainNull(items, "items");
if (items.length == 0) {
throw new IllegalArgumentException("`items` must not be empty");
return Uni.createFrom().failure(new IllegalArgumentException("`items` must not be empty"));
}
// Create command
RedisCommand cmd = RedisCommand.of(Command.TOPK_ADD)
Expand Down Expand Up @@ -106,7 +106,7 @@ Uni<Response> _topkQuery(K key, V... items) {
nonNull(key, "key");
doesNotContainNull(items, "items");
if (items.length == 0) {
throw new IllegalArgumentException("`items` must not be empty");
return Uni.createFrom().failure(new IllegalArgumentException("`items` must not be empty"));
}
// Create command
RedisCommand cmd = RedisCommand.of(Command.TOPK_QUERY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ public Uni<ReactiveRedisSubscriber> subscribe(List<String> channels, BiConsumer<

for (String channel : channels) {
if (channel == null) {
throw new IllegalArgumentException("Channels must not be null");
return Uni.createFrom().failure(new IllegalArgumentException("Channels must not be null"));
}
if (channel.isBlank()) {
throw new IllegalArgumentException("Channels cannot be blank");
return Uni.createFrom().failure(new IllegalArgumentException("Channels cannot be blank"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ public Uni<Void> execute(Command command, String... args) {
.map(r -> {
if (r == null || !r.toString().equals("QUEUED")) {
this.tx.discard();
throw new IllegalStateException("Unable to enqueue command into the current transaction");
return Uni.createFrom()
.failure(new IllegalStateException("Unable to enqueue command into the current transaction"));
}
return r;
})
Expand Down

0 comments on commit aadb627

Please sign in to comment.