diff --git a/library/src/main/java/dev/andstuff/kraken/api/endpoint/KrakenException.java b/library/src/main/java/dev/andstuff/kraken/api/endpoint/KrakenException.java index cb38aa8..87384ea 100644 --- a/library/src/main/java/dev/andstuff/kraken/api/endpoint/KrakenException.java +++ b/library/src/main/java/dev/andstuff/kraken/api/endpoint/KrakenException.java @@ -15,11 +15,12 @@ public class KrakenException extends RuntimeException { private final List errors; /** - * Creates an exception holding the errors returned by Kraken. + * Creates an exception holding the errors returned by Kraken, whose message lists them separated by commas. * * @param errors the errors, as returned by Kraken */ public KrakenException(List errors) { + super(errors == null ? null : String.join(", ", errors)); this.errors = errors; } } diff --git a/library/src/test/java/dev/andstuff/kraken/api/endpoint/KrakenExceptionTest.java b/library/src/test/java/dev/andstuff/kraken/api/endpoint/KrakenExceptionTest.java new file mode 100644 index 0000000..cbb026f --- /dev/null +++ b/library/src/test/java/dev/andstuff/kraken/api/endpoint/KrakenExceptionTest.java @@ -0,0 +1,36 @@ +package dev.andstuff.kraken.api.endpoint; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class KrakenExceptionTest { + + @Test + void should_use_error_as_message_when_kraken_returns_one_error() { + KrakenException unit = new KrakenException(List.of("EAPI:Invalid key")); + + assertThat(unit).hasMessage("EAPI:Invalid key"); + assertThat(unit.getErrors()).containsExactly("EAPI:Invalid key"); + } + + @Test + void should_join_errors_in_message_when_kraken_returns_several_errors() { + KrakenException unit = new KrakenException(List.of("EGeneral:Invalid arguments", "EGeneral:Permission denied")); + + assertThat(unit).hasMessage("EGeneral:Invalid arguments, EGeneral:Permission denied"); + } + + @Test + void should_have_no_message_when_kraken_returns_no_error_field() { + KrakenException unit = new KrakenException(null); + + assertThat(unit.getMessage()).isNull(); + assertThat(unit.getErrors()).isNull(); + } +}