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