Search in sources :

Example 1 with HandlerMethodFailedException

use of io.spine.server.model.HandlerMethodFailedException in project core-java by SpineEventEngine.

the class AggregateRepositoryShould method log_error_when_event_reaction_fails.

@Test
public void log_error_when_event_reaction_fails() {
    final FailingAggregateRepository repository = new FailingAggregateRepository();
    boundedContext.register(repository);
    final TestEventFactory factory = TestEventFactory.newInstance(getClass());
    // Passing negative float value should cause an exception.
    final EventEnvelope envelope = EventEnvelope.of(factory.createEvent(FloatValue.newBuilder().setValue(-412.0f).build()));
    boundedContext.getEventBus().post(envelope.getOuterObject());
    assertTrue(repository.isErrorLogged());
    final RuntimeException lastException = repository.getLastException();
    assertTrue(lastException instanceof HandlerMethodFailedException);
    final HandlerMethodFailedException methodFailedException = (HandlerMethodFailedException) lastException;
    assertEquals(envelope.getMessage(), methodFailedException.getDispatchedMessage());
    assertEquals(envelope.getEventContext(), methodFailedException.getMessageContext());
    final MessageEnvelope lastErrorEnvelope = repository.getLastErrorEnvelope();
    assertNotNull(lastErrorEnvelope);
    assertTrue(lastErrorEnvelope instanceof EventEnvelope);
    assertEquals(envelope.getMessage(), lastErrorEnvelope.getMessage());
}
Also used : EventEnvelope(io.spine.core.EventEnvelope) FailingAggregateRepository(io.spine.server.aggregate.given.AggregateRepositoryTestEnv.FailingAggregateRepository) TestEventFactory(io.spine.server.command.TestEventFactory) HandlerMethodFailedException(io.spine.server.model.HandlerMethodFailedException) MessageEnvelope(io.spine.core.MessageEnvelope) Test(org.junit.Test)

Example 2 with HandlerMethodFailedException

use of io.spine.server.model.HandlerMethodFailedException in project core-java by SpineEventEngine.

the class CommandHandlerMethodShould method set_producer_ID_if_entity.

@Test
public void set_producer_ID_if_entity() {
    final RefCreateProject commandMessage = createProject();
    final Aggregate<ProjectId, ?, ?> entity = new RejectingAggregate(commandMessage.getProjectId());
    final CommandEnvelope cmd = requestFactory.createEnvelope(commandMessage);
    try {
        AggregateMessageDispatcher.dispatchCommand(entity, cmd);
    } catch (HandlerMethodFailedException e) {
        assertCauseAndId(e, entity.getId());
    }
}
Also used : RefCreateProject(io.spine.test.reflect.command.RefCreateProject) HandlerMethodFailedException(io.spine.server.model.HandlerMethodFailedException) RejectingAggregate(io.spine.server.command.given.CommandHandlerMethodTestEnv.RejectingAggregate) ProjectId(io.spine.test.reflect.ProjectId) CommandEnvelope(io.spine.core.CommandEnvelope) Test(org.junit.Test)

Example 3 with HandlerMethodFailedException

use of io.spine.server.model.HandlerMethodFailedException in project core-java by SpineEventEngine.

the class CommandHandlerMethodShould method set_producer_ID_if_command_handler.

@Test
public void set_producer_ID_if_command_handler() {
    final CommandHandler handler = new RejectingHandler();
    final CommandEnvelope envelope = requestFactory.createEnvelope(createProject());
    try {
        handler.dispatch(envelope);
    } catch (HandlerMethodFailedException e) {
        assertCauseAndId(e, handler.getId());
    }
}
Also used : HandlerMethodFailedException(io.spine.server.model.HandlerMethodFailedException) CommandEnvelope(io.spine.core.CommandEnvelope) RejectingHandler(io.spine.server.command.given.CommandHandlerMethodTestEnv.RejectingHandler) Test(org.junit.Test)

Example 4 with HandlerMethodFailedException

use of io.spine.server.model.HandlerMethodFailedException in project core-java by SpineEventEngine.

the class CommandHandlerMethod method whyFailed.

/**
 * {@inheritDoc}
 *
 * <p>{@linkplain ThrowableMessage#initProducer(Any) Initializes} producer ID if the exception
 * was caused by a thrown rejection.
 */
@Override
protected HandlerMethodFailedException whyFailed(Object target, Message message, CommandContext context, Exception cause) {
    final HandlerMethodFailedException exception = super.whyFailed(target, message, context, cause);
    final Throwable rootCause = getRootCause(exception);
    if (rootCause instanceof ThrowableMessage) {
        final ThrowableMessage thrownMessage = (ThrowableMessage) rootCause;
        final Optional<Any> producerId = idOf(target);
        if (producerId.isPresent()) {
            thrownMessage.initProducer(producerId.get());
        }
    }
    return exception;
}
Also used : ThrowableMessage(io.spine.base.ThrowableMessage) HandlerMethodFailedException(io.spine.server.model.HandlerMethodFailedException) Any(com.google.protobuf.Any)

Aggregations

HandlerMethodFailedException (io.spine.server.model.HandlerMethodFailedException)4 Test (org.junit.Test)3 CommandEnvelope (io.spine.core.CommandEnvelope)2 Any (com.google.protobuf.Any)1 ThrowableMessage (io.spine.base.ThrowableMessage)1 EventEnvelope (io.spine.core.EventEnvelope)1 MessageEnvelope (io.spine.core.MessageEnvelope)1 FailingAggregateRepository (io.spine.server.aggregate.given.AggregateRepositoryTestEnv.FailingAggregateRepository)1 TestEventFactory (io.spine.server.command.TestEventFactory)1 RejectingAggregate (io.spine.server.command.given.CommandHandlerMethodTestEnv.RejectingAggregate)1 RejectingHandler (io.spine.server.command.given.CommandHandlerMethodTestEnv.RejectingHandler)1 ProjectId (io.spine.test.reflect.ProjectId)1 RefCreateProject (io.spine.test.reflect.command.RefCreateProject)1