Skip to content

Commit

Permalink
Improve error behavior (#15)
Browse files Browse the repository at this point in the history
* Executing a blocking oneshot command should not easily throw Exceptions, but return an error result instead.

* Javadocs
  • Loading branch information
d4rken authored Feb 4, 2018
1 parent 2a2bf7e commit bd4e7f4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
6 changes: 5 additions & 1 deletion core/src/main/java/eu/darken/rxshell/cmd/Cmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

Expand Down Expand Up @@ -300,9 +301,12 @@ public Single<Result> submit(RxCmdShell shell) {

/**
* Convenience method for {@link #submit(RxCmdShell)} using {@link Single#blockingGet()}
* <br>If a shell can't be opened {@link ExitCode#EXCEPTION} will be returned and an error message.
*/
public Result execute(RxCmdShell shell) {
return submit(shell).blockingGet();
return submit(shell)
.onErrorReturn(err -> new Result(build(), ExitCode.EXCEPTION, null, Collections.singletonList(err.toString())))
.blockingGet();
}
}

Expand Down
9 changes: 6 additions & 3 deletions core/src/test/java/eu/darken/rxshell/cmd/CmdBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,15 @@ public void testExecute_oneshot() {
verify(session).close();
}

@Test(expected = RuntimeException.class)
@Test
public void testExecute_oneshot_exception() throws IOException {
RxCmdShell shell = mock(RxCmdShell.class);
when(shell.open()).thenReturn(Single.error(new IOException()));
Exception ex = new IOException("Error message");
when(shell.open()).thenReturn(Single.error(ex));
when(shell.isAlive()).thenReturn(Single.just(false));

Cmd.builder("").execute(shell);
final Cmd.Result result = Cmd.builder("").execute(shell);
assertThat(result.getExitCode(), is(Cmd.ExitCode.EXCEPTION));
assertThat(result.getErrors(), contains(ex.toString()));
}
}

0 comments on commit bd4e7f4

Please sign in to comment.