Skip to content

Commit

Permalink
Add exception type for READONLY errors #1943
Browse files Browse the repository at this point in the history
Redis instances in read-only mode will reply with a `READONLY` error
message when a write is attempted. This change allows users to
specifically handle these errors.

Original pull request: #1944.
  • Loading branch information
anothertobi authored and mp911de committed Jan 4, 2022
1 parent e63f598 commit 0b70fb4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/main/java/io/lettuce/core/RedisReadOnlyException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.lettuce.core;

/**
* Exception that gets thrown when Redis replies with a {@code READONLY} error response.
*
* @author anothertobi
*/
@SuppressWarnings("serial")
public class RedisReadOnlyException extends RedisCommandExecutionException {

/**
* Create a {@code RedisReadOnlyException} with the specified detail message.
*
* @param msg the detail message.
*/
public RedisReadOnlyException(String msg) {
super(msg);
}

/**
* Create a {@code RedisReadOnlyException} with the specified detail message and nested exception.
*
* @param msg the detail message.
* @param cause the nested exception.
*/
public RedisReadOnlyException(String msg, Throwable cause) {
super(msg, cause);
}

}
4 changes: 4 additions & 0 deletions src/main/java/io/lettuce/core/internal/ExceptionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ public static RedisCommandExecutionException createExecutionException(String mes
return cause != null ? new RedisLoadingException(message, cause) : new RedisLoadingException(message);
}

if (message.startsWith("READONLY")) {
return cause != null ? new RedisReadOnlyException(message, cause) : new RedisReadOnlyException(message);
}

return cause != null ? new RedisCommandExecutionException(message, cause)
: new RedisCommandExecutionException(message);
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/io/lettuce/core/ExceptionFactoryUnitTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ void shouldCreateLoadingException() {
.hasRootCauseInstanceOf(IllegalStateException.class);
}

@Test
void shouldCreateReadOnlyException() {

assertThat(ExceptionFactory.createExecutionException("READONLY foo bar")).isInstanceOf(RedisReadOnlyException.class)
.hasMessage("READONLY foo bar").hasNoCause();
assertThat(ExceptionFactory.createExecutionException("READONLY foo bar", new IllegalStateException()))
.isInstanceOf(RedisReadOnlyException.class).hasMessage("READONLY foo bar")
.hasRootCauseInstanceOf(IllegalStateException.class);
}

@Test
void shouldFormatExactUnits() {

Expand Down

0 comments on commit 0b70fb4

Please sign in to comment.