Merge pull request #54 from kemitix/result-orelsethrow-checkederrorresultexception

[result] orElseThrow() throws error within a CheckedErrorResultException
This commit is contained in:
Paul Campbell 2018-10-06 20:11:14 +01:00 committed by GitHub
commit 80ffc53edc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 63 additions and 11 deletions

View file

@ -1,6 +1,11 @@
CHANGELOG
=========
1.2.0
-----
* [result] orElseThrow() throws error within a CheckedErrorResultException
1.1.0
-----

View file

@ -821,7 +821,7 @@
**** =T orElseThrow()=
Extracts the successful value from the result, or throws the error
=Throwable=.
within a =CheckedErrorResultException=.
#+BEGIN_SRC java
final Integer result = Result.of(() -> getValue())

View file

@ -0,0 +1,46 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 Paul Campbell
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies
* or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package net.kemitix.mon.result;
/**
* A checked wrapper for exceptions thrown within a {@link Result}.
*
* <p>Used by {@link Result#orElseThrow()} when the {@link Result} is an error.</p>
*
* @author Paul Campbell (pcampbell@kemitix.net)
*/
public final class CheckedErrorResultException extends Exception {
private CheckedErrorResultException(final Throwable cause) {
super(cause);
}
/**
* Creates a new object.
*
* @param cause the cause
* @return a {@link CheckedErrorResultException} containing the cause
*/
static CheckedErrorResultException with(final Throwable cause) {
return new CheckedErrorResultException(cause);
}
}

View file

@ -73,8 +73,8 @@ class Err<T> implements Result<T> {
}
@Override
public T orElseThrow() throws Throwable {
throw error;
public T orElseThrow() throws CheckedErrorResultException {
throw CheckedErrorResultException.with(error);
}
@Override

View file

@ -22,7 +22,7 @@
package net.kemitix.mon.result;
/**
* An unchecked wrapper fot exceptions thrown within a {@link Result}.
* An unchecked wrapper for exceptions thrown within a {@link Result}.
*
* <p>Used by {@link Result#orElseThrowUnchecked()} when the {@link Result} is an error.</p>
*

View file

@ -96,23 +96,21 @@ public interface Result<T> extends Functor<T, Result<?>> {
* @param <T> the type of the Maybe and the Result
* @return a Result containing the value of the Maybe when it is a Just, or the error when it is Nothing
*/
@SuppressWarnings("illegalcatch")
public static <T> Maybe<T> toMaybe(final Result<T> result) {
try {
return Maybe.just(result.orElseThrow());
} catch (final Throwable throwable) {
} catch (final CheckedErrorResultException throwable) {
return Maybe.nothing();
}
}
/**
* Extracts the successful value from the result, or throws the error Throwable.
* Extracts the successful value from the result, or throws the error within a {@link CheckedErrorResultException}.
*
* @return the value if a success
* @throws Throwable if the result is an error
* @throws CheckedErrorResultException if the result is an error
*/
@SuppressWarnings("illegalthrows")
public abstract T orElseThrow() throws Throwable;
public abstract T orElseThrow() throws CheckedErrorResultException;
/**
* Extracts the successful value from the result, or throws the error Throwable.

View file

@ -2,6 +2,7 @@ package net.kemitix.mon;
import lombok.RequiredArgsConstructor;
import net.kemitix.mon.maybe.Maybe;
import net.kemitix.mon.result.CheckedErrorResultException;
import net.kemitix.mon.result.ErrorResultException;
import net.kemitix.mon.result.UnexpectedErrorResultException;
import net.kemitix.mon.result.Result;
@ -294,7 +295,9 @@ public class ResultTest implements WithAssertions {
final RuntimeException exception = new RuntimeException();
final Result<Integer> error = Result.error(exception);
//when
assertThatThrownBy(() -> error.orElseThrow()).isSameAs(exception);
assertThatThrownBy(() -> error.orElseThrow())
.isInstanceOf(CheckedErrorResultException.class)
.hasCause(exception);
}
@Test public void okay_whenOrElseThrowT_isValue() throws Exception {