Skip to content

Commit

Permalink
Add Unexpected type (#29)
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 02defdd commit 46149c8
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
85 changes: 85 additions & 0 deletions common-types/src/main/java/io/carbynestack/common/Unexpected.java
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;
}
}
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();
}
}

0 comments on commit 46149c8

Please sign in to comment.