-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: David Greven <[email protected]> Signed-off-by: Sebastian Becker <[email protected]>
- Loading branch information
1 parent
02defdd
commit 46149c8
Showing
2 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
common-types/src/main/java/io/carbynestack/common/Unexpected.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright (c) 2021 - for information on the respective copyright owner | ||
* see the NOTICE file and/or the repository https://github.com/carbynestack/common. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package io.carbynestack.common; | ||
|
||
import java.util.Optional; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static java.util.Optional.ofNullable; | ||
|
||
/** | ||
* Represents an unexpected {@link Throwable} based failure reason. | ||
* | ||
* @version JDK 8 | ||
* @see CsFailureReason | ||
* @since 0.2.0 | ||
*/ | ||
public final class Unexpected implements CsFailureReason { | ||
/** | ||
* The failure reason. | ||
* | ||
* @since 0.2.0 | ||
*/ | ||
private final Throwable throwable; | ||
|
||
/** | ||
* Creates an {@code Unexpected} instance from a {@code Throwable}. | ||
* | ||
* @param throwable the failure reason | ||
* @since 0.2.0 | ||
*/ | ||
public Unexpected(Throwable throwable) { | ||
this.throwable = requireNonNull(throwable); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @return the description synopsis | ||
* @see #description() | ||
* @since 0.2.0 | ||
*/ | ||
@Override | ||
public String synopsis() { | ||
return "An unknown exception has occurred."; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @return the full description | ||
* @see #synopsis() | ||
* @since 0.2.0 | ||
*/ | ||
@Override | ||
public String description() { | ||
return throwable.getLocalizedMessage(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @return the stack trace as an {@link Optional} | ||
* @since 0.2.0 | ||
*/ | ||
@Override | ||
public Optional<StackTraceElement[]> stackTrace() { | ||
return ofNullable(throwable.getStackTrace()); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @return {@code true} if the request should be triggered, | ||
* otherwise {@code false}. | ||
* @since 0.2.0 | ||
*/ | ||
@Override | ||
public boolean reportIssue() { | ||
return true; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
common-types/src/test/java/io/carbynestack/common/UnexpectedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (c) 2021 - for information on the respective copyright owner | ||
* see the NOTICE file and/or the repository https://github.com/carbynestack/common. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package io.carbynestack.common; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
class UnexpectedTest { | ||
private final Unexpected unknown = new Unexpected(new IOException("test")); | ||
|
||
@Test | ||
void constructor() { | ||
assertThatThrownBy(() -> new Unexpected(null)).isExactlyInstanceOf(NullPointerException.class); | ||
} | ||
|
||
@Test | ||
void synopsis() { | ||
assertThat(unknown.synopsis()).isEqualTo("An unknown exception has occurred."); | ||
} | ||
|
||
@Test | ||
void description() { | ||
assertThat(unknown.description()).isEqualTo("test"); | ||
} | ||
|
||
@Test | ||
void stackTrace() { | ||
assertThat(unknown.stackTrace()).isNotEmpty(); | ||
} | ||
|
||
@Test | ||
void reportIssue() { | ||
assertThat(unknown.reportIssue()).isTrue(); | ||
} | ||
} |