Skip to content

Commit

Permalink
Add CsFailureThrowable type (#27)
Browse files Browse the repository at this point in the history
Signed-off-by: David Greven <[email protected]>
Signed-off-by: Sebastian Becker <[email protected]>
  • Loading branch information
grevend-bosch authored Dec 13, 2021
1 parent 9ea08c8 commit 02defdd
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
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();
}
}
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[]";
}
}
}

0 comments on commit 02defdd

Please sign in to comment.