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;
}
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);
}
}
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;
}
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();
}
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))));
}
Aggregations