-
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
9ea08c8
commit 02defdd
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
common-types/src/main/java/io/carbynestack/common/CsFailureThrowable.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,51 @@ | ||
/* | ||
* 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 io.carbynestack.common.result.Failure; | ||
|
||
import java.util.Optional; | ||
|
||
import static java.util.Optional.empty; | ||
import static java.util.Optional.ofNullable; | ||
|
||
/** | ||
* Represents the Carbyne Stack throwable failure base type. | ||
* | ||
* @version JDK 8 | ||
* @see Failure | ||
* @see CsFailureReason | ||
* @since 0.2.0 | ||
*/ | ||
public interface CsFailureThrowable extends CsFailureReason { | ||
/** | ||
* {@inheritDoc} | ||
* | ||
* @return the description synopsis | ||
* @see #description() | ||
* @since 0.2.0 | ||
*/ | ||
@Override | ||
default String synopsis() { | ||
return this instanceof Throwable | ||
? ((Throwable) this).getLocalizedMessage() | ||
: "Throwable %s has been caught.".formatted(this); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @return the stack trace as an {@link Optional} | ||
* @since 0.2.0 | ||
*/ | ||
@Override | ||
default Optional<StackTraceElement[]> stackTrace() { | ||
return this instanceof Throwable | ||
? ofNullable(((Throwable) this).getStackTrace()) | ||
: empty(); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
common-types/src/test/java/io/carbynestack/common/CsFailureThrowableTest.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,56 @@ | ||
/* | ||
* 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 static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class CsFailureThrowableTest { | ||
@Test | ||
void throwableSynopsis() { | ||
assertThat(new SomeThrowable().synopsis()).isEqualTo("Some message."); | ||
} | ||
|
||
@Test | ||
void nonThrowableSynopsis() { | ||
assertThat(new NonThrowable().synopsis()).isEqualTo("Throwable NonThrowable[] has been caught."); | ||
} | ||
|
||
@Test | ||
void throwableStackTrace() { | ||
try { | ||
throw new SomeThrowable(); | ||
} catch (SomeThrowable throwable) { | ||
assertThat(throwable.stackTrace()).isNotEmpty(); | ||
} | ||
} | ||
|
||
@Test | ||
void nonThrowableStackTrace() { | ||
assertThat(new NonThrowable().stackTrace()).isEmpty(); | ||
} | ||
|
||
static final class NonThrowable implements CsFailureThrowable { | ||
@Override | ||
public String toString() { | ||
return "NonThrowable[]"; | ||
} | ||
} | ||
|
||
static final class SomeThrowable extends Exception implements CsFailureThrowable { | ||
@Override | ||
public String getMessage() { | ||
return "Some message."; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "SomeThrowable[]"; | ||
} | ||
} | ||
} |