diff --git a/src/main/java/com/lambdaworks/redis/ExceptionFactory.java b/src/main/java/com/lambdaworks/redis/ExceptionFactory.java index d9a47a35a5..8f34ba5543 100644 --- a/src/main/java/com/lambdaworks/redis/ExceptionFactory.java +++ b/src/main/java/com/lambdaworks/redis/ExceptionFactory.java @@ -19,7 +19,7 @@ /** * Factory for Redis exceptions. - * + * * @author Mark Paluch * @since 4.5 */ @@ -30,7 +30,7 @@ private ExceptionFactory() { /** * Create a {@link RedisCommandTimeoutException} with a detail message given the timeout. - * + * * @param timeout the timeout value. * @param unit the {@link TimeUnit}. * @return the {@link RedisCommandTimeoutException}. @@ -41,7 +41,7 @@ public static RedisCommandTimeoutException createTimeoutException(long timeout, /** * Create a {@link RedisCommandTimeoutException} with a detail message given the message and timeout. - * + * * @param message the detail message. * @param timeout the timeout value. * @param unit the {@link TimeUnit}. @@ -54,7 +54,7 @@ public static RedisCommandTimeoutException createTimeoutException(String message /** * Create a {@link RedisCommandExecutionException} with a detail message. Specific Redis error messages may create subtypes * of {@link RedisCommandExecutionException}. - * + * * @param message the detail message. * @return the {@link RedisCommandExecutionException}. */ @@ -65,7 +65,7 @@ public static RedisCommandExecutionException createExecutionException(String mes /** * Create a {@link RedisCommandExecutionException} with a detail message and optionally a {@link Throwable cause}. Specific * Redis error messages may create subtypes of {@link RedisCommandExecutionException}. - * + * * @param message the detail message. * @param cause the nested exception, may be {@literal null}. * @return the {@link RedisCommandExecutionException}. @@ -82,6 +82,10 @@ public static RedisCommandExecutionException createExecutionException(String mes return cause != null ? new RedisNoScriptException(message, cause) : new RedisNoScriptException(message); } + if (message.startsWith("LOADING")) { + return cause != null ? new RedisLoadingException(message, cause) : new RedisLoadingException(message); + } + return cause != null ? new RedisCommandExecutionException(message, cause) : new RedisCommandExecutionException( message); } diff --git a/src/main/java/com/lambdaworks/redis/RedisLoadingException.java b/src/main/java/com/lambdaworks/redis/RedisLoadingException.java new file mode 100644 index 0000000000..9457f7c53f --- /dev/null +++ b/src/main/java/com/lambdaworks/redis/RedisLoadingException.java @@ -0,0 +1,45 @@ +/* + * Copyright 2018 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 + * + * http://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 com.lambdaworks.redis; + +/** + * Exception that gets thrown when Redis is loading a dataset into memory and replying with a {@code LOADING} error response. + * + * @author Mark Paluch + * @since 4.5 + */ +@SuppressWarnings("serial") +public class RedisLoadingException extends RedisCommandExecutionException { + + /** + * Create a {@code RedisLoadingException} with the specified detail message. + * + * @param msg the detail message. + */ + public RedisLoadingException(String msg) { + super(msg); + } + + /** + * Create a {@code RedisLoadingException} with the specified detail message and nested exception. + * + * @param msg the detail message. + * @param cause the nested exception. + */ + public RedisLoadingException(String msg, Throwable cause) { + super(msg, cause); + } +} diff --git a/src/test/java/com/lambdaworks/redis/ExceptionFactoryTest.java b/src/test/java/com/lambdaworks/redis/ExceptionFactoryTest.java index 82ad31dd8c..03438a1fc7 100644 --- a/src/test/java/com/lambdaworks/redis/ExceptionFactoryTest.java +++ b/src/test/java/com/lambdaworks/redis/ExceptionFactoryTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 the original author or authors. + * Copyright 2017-2018 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. @@ -55,4 +55,14 @@ public void shouldCreateExecutionException() { assertThat(ExceptionFactory.createExecutionException(null, new IllegalStateException())).isInstanceOf( RedisCommandExecutionException.class).hasRootCauseInstanceOf(IllegalStateException.class); } -} \ No newline at end of file + + @Test + public void shouldCreateLoadingException() { + + assertThat(ExceptionFactory.createExecutionException("LOADING foo bar")).isInstanceOf(RedisLoadingException.class) + .hasMessage("LOADING foo bar").hasNoCause(); + assertThat(ExceptionFactory.createExecutionException("LOADING foo bar", new IllegalStateException())) + .isInstanceOf(RedisLoadingException.class).hasMessage("LOADING foo bar") + .hasRootCauseInstanceOf(IllegalStateException.class); + } +}