Search in sources :

Example 1 with ThrowableMessage

use of io.spine.base.ThrowableMessage in project core-java by SpineEventEngine.

the class CommandBus method doPost.

@Override
protected Ack doPost(CommandEnvelope envelope) {
    final CommandDispatcher<?> dispatcher = getDispatcher(envelope);
    Ack result;
    try {
        dispatcher.dispatch(envelope);
        commandStore.setCommandStatusOk(envelope);
        result = acknowledge(envelope.getId());
    } catch (RuntimeException e) {
        final Throwable cause = getRootCause(e);
        commandStore.updateCommandStatus(envelope, cause, log);
        if (causedByRejection(e)) {
            final ThrowableMessage throwableMessage = (ThrowableMessage) cause;
            final Rejection rejection = toRejection(throwableMessage, envelope.getCommand());
            final Class<?> rejectionClass = AnyPacker.unpack(rejection.getMessage()).getClass();
            Log.log().trace("Posting rejection {} to RejectionBus.", rejectionClass.getName());
            rejectionBus().post(rejection);
            result = reject(envelope.getId(), rejection);
        } else {
            final Error error = toError(cause);
            result = reject(envelope.getId(), error);
        }
    }
    return result;
}
Also used : Rejections.toRejection(io.spine.core.Rejections.toRejection) Rejections.causedByRejection(io.spine.core.Rejections.causedByRejection) Rejection(io.spine.core.Rejection) ThrowableMessage(io.spine.base.ThrowableMessage) Ack(io.spine.core.Ack) Exceptions.toError(io.spine.util.Exceptions.toError) Error(io.spine.base.Error) CommandClass(io.spine.core.CommandClass)

Example 2 with ThrowableMessage

use of io.spine.base.ThrowableMessage in project core-java by SpineEventEngine.

the class CommandStore method updateCommandStatus.

// OK for this consolidated error handling.
@SuppressWarnings("ChainOfInstanceofChecks")
public void updateCommandStatus(CommandEnvelope commandEnvelope, Throwable cause, Log log) {
    final Message commandMessage = commandEnvelope.getMessage();
    final CommandId commandId = commandEnvelope.getId();
    if (cause instanceof ThrowableMessage) {
        final ThrowableMessage throwableMessage = (ThrowableMessage) cause;
        log.rejectedWith(throwableMessage, commandMessage, commandId);
        updateStatus(commandEnvelope, toRejection(throwableMessage, commandEnvelope.getCommand()));
    } else if (cause instanceof Exception) {
        final Exception exception = (Exception) cause;
        log.errorHandling(exception, commandMessage, commandId);
        updateStatus(commandEnvelope, exception);
    } else {
        log.errorHandlingUnknown(cause, commandMessage, commandId);
        final Error error = Errors.fromThrowable(cause);
        updateStatus(commandEnvelope, error);
    }
}
Also used : ThrowableMessage(io.spine.base.ThrowableMessage) ThrowableMessage(io.spine.base.ThrowableMessage) Message(com.google.protobuf.Message) Error(io.spine.base.Error) CommandId(io.spine.core.CommandId) CommandException(io.spine.server.commandbus.CommandException)

Example 3 with ThrowableMessage

use of io.spine.base.ThrowableMessage in project core-java by SpineEventEngine.

the class Commands method rejectWithCause.

/**
 * Produces a {@link Rejection} for the given {@link Command} based on the given
 * {@linkplain ThrowableMessage cause}.
 *
 * <p>The given {@link Throwable} should be
 * {@linkplain Rejections#causedByRejection caused by a Rejection} or
 * an {@link IllegalArgumentException} is thrown.
 *
 * @param command the command to reject
 * @param cause the rejection cause (may be wrapped into other kinds of {@code Throwable})
 * @return a {@link Rejection} for the given command
 * @throws IllegalArgumentException upon an invalid rejection cause
 */
@Internal
public static Rejection rejectWithCause(Command command, Throwable cause) throws IllegalArgumentException {
    checkNotNull(command);
    checkNotNull(cause);
    final ThrowableMessage rejectionThrowable = Rejections.getCause(cause);
    final Rejection rejection = Rejections.toRejection(rejectionThrowable, command);
    return rejection;
}
Also used : ThrowableMessage(io.spine.base.ThrowableMessage) Internal(io.spine.annotation.Internal)

Example 4 with ThrowableMessage

use of io.spine.base.ThrowableMessage in project core-java by SpineEventEngine.

the class Rejections method toRejection.

/**
 * Converts this {@code ThrowableMessage} into {@link Rejection}.
 *
 * @param command the command which caused the rejection
 */
public static Rejection toRejection(ThrowableMessage throwable, Command command) {
    checkNotNull(throwable);
    checkNotNull(command);
    final Message rejectionMessage = throwable.getMessageThrown();
    final Any packedState = pack(rejectionMessage);
    final RejectionContext context = createContext(throwable, command);
    final RejectionId id = generateId(command.getId());
    final Rejection.Builder builder = Rejection.newBuilder().setId(id).setMessage(packedState).setContext(context);
    return builder.build();
}
Also used : ThrowableMessage(io.spine.base.ThrowableMessage) Message(com.google.protobuf.Message) Any(com.google.protobuf.Any)

Example 5 with ThrowableMessage

use of io.spine.base.ThrowableMessage in project core-java by SpineEventEngine.

the class RejectionsShould method tell_if_RuntimeException_was_called_by_command_rejection.

@SuppressWarnings({ "NewExceptionWithoutArguments", /* No need to have a message for this test. */
"SerializableInnerClassWithNonSerializableOuterClass" /* Does not refer anything. */
})
@Test
public void tell_if_RuntimeException_was_called_by_command_rejection() {
    assertFalse(causedByRejection(new RuntimeException()));
    final ThrowableMessage throwableMessage = new ThrowableMessage(Time.getCurrentTime()) {

        private static final long serialVersionUID = 0L;
    };
    assertTrue(causedByRejection(new IllegalStateException(throwableMessage)));
    // Check that root cause is analyzed.
    assertTrue(causedByRejection(new RuntimeException(new IllegalStateException(throwableMessage))));
}
Also used : ThrowableMessage(io.spine.base.ThrowableMessage) Test(org.junit.Test)

Aggregations

ThrowableMessage (io.spine.base.ThrowableMessage)7 Any (com.google.protobuf.Any)2 Message (com.google.protobuf.Message)2 Error (io.spine.base.Error)2 Internal (io.spine.annotation.Internal)1 Ack (io.spine.core.Ack)1 CommandClass (io.spine.core.CommandClass)1 CommandId (io.spine.core.CommandId)1 Rejection (io.spine.core.Rejection)1 Rejections.causedByRejection (io.spine.core.Rejections.causedByRejection)1 Rejections.toRejection (io.spine.core.Rejections.toRejection)1 CommandException (io.spine.server.commandbus.CommandException)1 HandlerMethodFailedException (io.spine.server.model.HandlerMethodFailedException)1 Exceptions.toError (io.spine.util.Exceptions.toError)1 Test (org.junit.Test)1