Search in sources :

Example 6 with Error

use of io.spine.base.Error 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.getCommandId();
    if (cause instanceof FailureThrowable) {
        final FailureThrowable failure = (FailureThrowable) cause;
        log.failureHandling(failure, commandMessage, commandId);
        updateStatus(commandEnvelope, failure.toFailure(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 : Message(com.google.protobuf.Message) FailureThrowable(io.spine.base.FailureThrowable) Error(io.spine.base.Error) CommandId(io.spine.base.CommandId) CommandException(io.spine.server.commandbus.CommandException)

Example 7 with Error

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

the class StatusesShould method create_invalid_argument_status_exception.

@Test
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
public void create_invalid_argument_status_exception() {
    final IllegalArgumentException exception = new IllegalArgumentException("");
    final Error expectedError = Error.getDefaultInstance();
    final StatusRuntimeException statusRuntimeEx = invalidArgumentWithCause(exception, expectedError);
    final Error actualError = MetadataConverter.toError(statusRuntimeEx.getTrailers()).get();
    assertEquals(Status.INVALID_ARGUMENT.getCode(), statusRuntimeEx.getStatus().getCode());
    assertEquals(exception, statusRuntimeEx.getCause());
    assertEquals(expectedError, actualError);
}
Also used : StatusRuntimeException(io.grpc.StatusRuntimeException) Error(io.spine.base.Error) Test(org.junit.Test)

Example 8 with Error

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

the class InvalidCommandException method onMissingTenantId.

/**
     * Creates an exception for a command with missing {@code tenant_id} attribute in
     * the {@code CommandContext}, which is required in a multitenant application.
     */
public static InvalidCommandException onMissingTenantId(Command command) {
    final CommandEnvelope envelope = CommandEnvelope.of(command);
    final Message commandMessage = envelope.getMessage();
    final String errMsg = format("The command (class: `%s`, type: `%s`, id: `%s`) is posted to " + "multitenant Command Bus, but has no `tenant_id` attribute in the context.", CommandClass.of(commandMessage).value().getName(), TypeName.of(commandMessage), idToString(envelope.getCommandId()));
    final Error error = unknownTenantError(commandMessage, errMsg);
    return new InvalidCommandException(errMsg, command, error);
}
Also used : Message(com.google.protobuf.Message) CommandEnvelope(io.spine.envelope.CommandEnvelope) CommandValidationError(io.spine.base.CommandValidationError) Error(io.spine.base.Error) Identifiers.idToString(io.spine.base.Identifiers.idToString)

Example 9 with Error

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

the class InvalidCommandException method onInapplicableTenantId.

public static InvalidCommandException onInapplicableTenantId(Command command) {
    final CommandEnvelope cmd = CommandEnvelope.of(command);
    final TypeName typeName = TypeName.of(cmd.getMessage());
    final String errMsg = format("The command (class: %s, type: %s, id: %s) was posted to single-tenant " + "CommandBus, but has tenant_id: %s attribute set in the command context.", cmd.getMessageClass(), typeName, cmd.getCommandId(), cmd.getTenantId());
    final Error error = inapplicableTenantError(cmd.getMessage(), errMsg);
    return new InvalidCommandException(errMsg, command, error);
}
Also used : TypeName(io.spine.type.TypeName) CommandEnvelope(io.spine.envelope.CommandEnvelope) CommandValidationError(io.spine.base.CommandValidationError) Error(io.spine.base.Error) Identifiers.idToString(io.spine.base.Identifiers.idToString)

Example 10 with Error

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

the class Rescheduler method onScheduledCommandExpired.

/**
     * Sets the status of the expired command to error.
     *
     * <p>We cannot post such a command because there is no handler or dispatcher registered yet.
     * Or, posting such a command may be undesirable from the business logic point of view.
     *
     * @param command the expired command
     * @see CommandExpiredException
     */
private void onScheduledCommandExpired(Command command) {
    final CommandEnvelope commandEnvelope = CommandEnvelope.of(command);
    final Message msg = commandEnvelope.getMessage();
    final CommandId id = commandEnvelope.getCommandId();
    final Error error = CommandExpiredException.commandExpiredError(msg);
    commandStore().setToError(commandEnvelope, error);
    log().errorExpiredCommand(msg, id);
}
Also used : Message(com.google.protobuf.Message) CommandEnvelope(io.spine.envelope.CommandEnvelope) Error(io.spine.base.Error) CommandId(io.spine.base.CommandId)

Aggregations

Error (io.spine.base.Error)14 Test (org.junit.Test)7 Message (com.google.protobuf.Message)4 CommandId (io.spine.base.CommandId)4 CommandEnvelope (io.spine.envelope.CommandEnvelope)4 Command (io.spine.base.Command)3 CommandValidationError (io.spine.base.CommandValidationError)3 Identifiers.idToString (io.spine.base.Identifiers.idToString)3 CommandRecord (io.spine.server.commandbus.CommandRecord)3 TenantAwareTest (io.spine.server.tenant.TenantAwareTest)3 ProtocolMessageEnum (com.google.protobuf.ProtocolMessageEnum)2 Metadata (io.grpc.Metadata)2 ValidationError (io.spine.validate.ValidationError)2 Duration (com.google.protobuf.Duration)1 Timestamp (com.google.protobuf.Timestamp)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 Commands.getMessage (io.spine.base.Commands.getMessage)1 FailureThrowable (io.spine.base.FailureThrowable)1 CommandException (io.spine.server.commandbus.CommandException)1 CommandMessage.createProjectMessage (io.spine.server.commandbus.Given.CommandMessage.createProjectMessage)1