Skip to content

Commit

Permalink
Change failure converter interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Quinn-With-Two-Ns committed Jan 13, 2025
1 parent 8cb9697 commit b848aec
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import io.temporal.api.failure.v1.Failure;
import io.temporal.api.failure.v1.ResetWorkflowFailureInfo;
import io.temporal.api.failure.v1.TimeoutFailureInfo;
import io.temporal.failure.TemporalFailure;
import io.temporal.payload.codec.ChainCodec;
import io.temporal.payload.codec.PayloadCodec;
import io.temporal.payload.context.SerializationContext;
Expand Down Expand Up @@ -199,7 +198,7 @@ public Failure exceptionToFailure(@Nonnull Throwable throwable) {

@Override
@Nonnull
public TemporalFailure failureToException(@Nonnull Failure failure) {
public RuntimeException failureToException(@Nonnull Failure failure) {
Preconditions.checkNotNull(failure, "failure");
return ConverterUtils.withContext(dataConverter, serializationContext)
.failureToException(this.decodeFailure(failure.toBuilder()).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import io.temporal.api.failure.v1.Failure;
import io.temporal.common.Experimental;
import io.temporal.failure.DefaultFailureConverter;
import io.temporal.failure.TemporalFailure;
import io.temporal.payload.codec.PayloadCodec;
import io.temporal.payload.context.SerializationContext;
import java.lang.reflect.Type;
Expand Down Expand Up @@ -176,7 +175,7 @@ default Object[] fromPayloads(
* @throws NullPointerException if failure is null
*/
@Nonnull
default TemporalFailure failureToException(@Nonnull Failure failure) {
default RuntimeException failureToException(@Nonnull Failure failure) {
Preconditions.checkNotNull(failure, "failure");
return new DefaultFailureConverter().failureToException(failure, this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import io.temporal.api.failure.v1.Failure;
import io.temporal.failure.DefaultFailureConverter;
import io.temporal.failure.TemporalFailure;
import io.temporal.payload.context.SerializationContext;
import javax.annotation.Nonnull;

Expand All @@ -49,7 +48,7 @@ public interface FailureConverter {
* @throws NullPointerException if either failure or dataConverter is null
*/
@Nonnull
TemporalFailure failureToException(
RuntimeException failureToException(
@Nonnull Failure failure, @Nonnull DataConverter dataConverter);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import io.temporal.api.common.v1.Payloads;
import io.temporal.api.failure.v1.Failure;
import io.temporal.failure.DefaultFailureConverter;
import io.temporal.failure.TemporalFailure;
import io.temporal.payload.context.SerializationContext;
import java.lang.reflect.Type;
import java.util.*;
Expand Down Expand Up @@ -135,7 +134,7 @@ public <T> T fromPayloads(

@Override
@Nonnull
public TemporalFailure failureToException(@Nonnull Failure failure) {
public RuntimeException failureToException(@Nonnull Failure failure) {
Preconditions.checkNotNull(failure, "failure");
return (serializationContext != null
? failureConverter.withContext(serializationContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,23 @@ public final class DefaultFailureConverter implements FailureConverter {

@Override
@Nonnull
public TemporalFailure failureToException(
public RuntimeException failureToException(
@Nonnull Failure failure, @Nonnull DataConverter dataConverter) {
Preconditions.checkNotNull(failure, "failure");
Preconditions.checkNotNull(dataConverter, "dataConverter");
TemporalFailure result = failureToExceptionImpl(failure, dataConverter);
result.setFailure(failure);
RuntimeException result = failureToExceptionImpl(failure, dataConverter);
if (result instanceof TemporalFailure) {
((TemporalFailure) result).setFailure(failure);
}
if (failure.getSource().equals(JAVA_SDK) && !failure.getStackTrace().isEmpty()) {
StackTraceElement[] stackTrace = parseStackTrace(failure.getStackTrace());
result.setStackTrace(stackTrace);
}
return result;
}

private TemporalFailure failureToExceptionImpl(Failure failure, DataConverter dataConverter) {
TemporalFailure cause =
private RuntimeException failureToExceptionImpl(Failure failure, DataConverter dataConverter) {
Exception cause =
failure.hasCause() ? failureToException(failure.getCause(), dataConverter) : null;
switch (failure.getFailureInfoCase()) {
case APPLICATION_FAILURE_INFO:
Expand Down Expand Up @@ -191,7 +193,8 @@ private TemporalFailure failureToExceptionImpl(Failure failure, DataConverter da
case NEXUS_HANDLER_FAILURE_INFO:
{
NexusHandlerFailureInfo info = failure.getNexusHandlerFailureInfo();
return new OperationHandlerException(OperationHandlerException.ErrorType.valueOf(info.getType()), cause);
return new OperationHandlerException(
OperationHandlerException.ErrorType.valueOf(info.getType()), cause);
}
case FAILUREINFO_NOT_SET:
default:
Expand Down Expand Up @@ -318,8 +321,8 @@ private Failure exceptionToFailure(Throwable throwable) {
failure.setNexusOperationExecutionFailureInfo(op);
} else if (throwable instanceof OperationHandlerException) {
OperationHandlerException oe = (OperationHandlerException) throwable;
NexusHandlerFailureInfo.Builder info = NexusHandlerFailureInfo.newBuilder()
.setType(oe.getErrorType().toString());
NexusHandlerFailureInfo.Builder info =
NexusHandlerFailureInfo.newBuilder().setType(oe.getErrorType().toString());
failure.setNexusHandlerFailureInfo(info);
} else {
ApplicationFailureInfo.Builder info =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@

import static io.temporal.internal.common.NexusUtil.nexusProtoLinkToLink;

import com.google.protobuf.ByteString;
import com.uber.m3.tally.Scope;
import io.nexusrpc.FailureInfo;
import io.nexusrpc.Header;
import io.nexusrpc.OperationUnsuccessfulException;
import io.nexusrpc.handler.*;
Expand Down Expand Up @@ -182,9 +180,10 @@ public Result handle(NexusTask task, Scope metricsScope) throws TimeoutException
private Failure createFailure(Throwable exception) {
io.temporal.api.failure.v1.Failure failure = dataConverter.exceptionToFailure(exception);
return Failure.newBuilder()
.setMessage(failure.getMessage())
.setDetails(failure.toByteString())
.putAllMetadata(Collections.singletonMap("type", "NexusFailureType")).build();
.setMessage(failure.getMessage())
.setDetails(failure.toByteString())
.putAllMetadata(Collections.singletonMap("type", "NexusFailureType"))
.build();
}

private void cancelOperation(OperationContext context, OperationCancelDetails details) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ public OperationStartResult<R> start(
return result.build();
} catch (URISyntaxException e) {
// Not expected as the link is constructed by the SDK.
throw new OperationHandlerException(
OperationHandlerException.ErrorType.INTERNAL, e);
throw new OperationHandlerException(OperationHandlerException.ErrorType.INTERNAL, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public void testMessageAndStackTraceAreCorrectlyDecoded() {
throw ApplicationFailure.newFailureWithCause("Message", "Type", causeException);
} catch (ApplicationFailure originalException) {
Failure failure = dataConverter.exceptionToFailure(originalException);
TemporalFailure decodedException = dataConverter.failureToException(failure);
TemporalFailure decodedException =
(TemporalFailure) dataConverter.failureToException(failure);

assertEquals("Message", decodedException.getOriginalMessage());
assertEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,15 @@ public OperationHandler<String, String> operation() {
details.getRequestId(),
invocationCount.getOrDefault(details.getRequestId(), 0) + 1);
if (invocationCount.get(details.getRequestId()) > 1) {
throw new OperationUnsuccessfulException("exceeded invocation count");
throw OperationUnsuccessfulException.Failure(
new RuntimeException("exceeded invocation count"));
}
switch (operation) {
case "success":
return operation;
case "fail":
throw new OperationUnsuccessfulException("fail");
throw OperationUnsuccessfulException.Failure(
new RuntimeException("intentional failure"));
case "handlererror":
throw new OperationHandlerException(
OperationHandlerException.ErrorType.BAD_REQUEST, "handlererror");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ public OperationHandler<String, String> operation() {
// Implemented inline
return OperationHandler.sync(
(ctx, details, name) -> {
throw new OperationUnsuccessfulException("failed to call operation");
throw OperationUnsuccessfulException.Failure(
new RuntimeException("failed to call operation"));
});
}
}
Expand Down

0 comments on commit b848aec

Please sign in to comment.