diff --git a/src/main/java/com/hivemq/cli/commands/cli/SubscribeCommand.java b/src/main/java/com/hivemq/cli/commands/cli/SubscribeCommand.java index 11084ab53..4656221d5 100644 --- a/src/main/java/com/hivemq/cli/commands/cli/SubscribeCommand.java +++ b/src/main/java/com/hivemq/cli/commands/cli/SubscribeCommand.java @@ -24,7 +24,6 @@ import com.hivemq.cli.mqtt.MqttClientExecutor; import com.hivemq.cli.utils.LoggerUtils; import com.hivemq.client.mqtt.MqttClient; -import com.hivemq.client.mqtt.exceptions.ConnectionFailedException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.tinylog.Logger; @@ -109,7 +108,7 @@ public SubscribeCommand(final @NotNull MqttClientExecutor mqttClientExecutor) { try { mqttClientExecutor.subscribe(subscribeClient, subscribeOptions); - } catch (final ConnectionFailedException exception) { + } catch (final Exception exception) { LoggerUtils.logCommandError("Unable to subscribe", exception, debugOptions); return 1; } diff --git a/src/main/java/com/hivemq/cli/mqtt/MqttClientExecutor.java b/src/main/java/com/hivemq/cli/mqtt/MqttClientExecutor.java index e0d8c97f4..14faa2134 100644 --- a/src/main/java/com/hivemq/cli/mqtt/MqttClientExecutor.java +++ b/src/main/java/com/hivemq/cli/mqtt/MqttClientExecutor.java @@ -118,14 +118,14 @@ void mqtt5Subscribe( .subscribe(subscribeMessage, new SubscribeMqtt5PublishCallback(subscribeOptions, client)) .whenComplete((subAck, throwable) -> { if (throwable != null) { - Logger.error(throwable, - "{} failed SUBSCRIBE to TOPIC '{}': {}", + Logger.error("{} failed SUBSCRIBE to TOPIC '{}': {}", clientLogPrefix, topic, Throwables.getRootCause(throwable).getMessage()); if (throwable instanceof Mqtt5SubAckException) { Logger.debug(((Mqtt5SubAckException) throwable).getMqttMessage()); } + Logger.trace(throwable); } else { final ClientKey clientKey = ClientKey.of(client); getClientDataMap().get(clientKey).addSubscription(MqttTopicFilter.of(topic)); @@ -150,14 +150,14 @@ void mqtt3Subscribe( .subscribe(subscribeMessage, new SubscribeMqtt3PublishCallback(subscribeOptions, client)) .whenComplete((subAck, throwable) -> { if (throwable != null) { - Logger.error(throwable, - "{} failed SUBSCRIBE to TOPIC '{}': {}", + Logger.error("{} failed SUBSCRIBE to TOPIC '{}': {}", clientLogPrefix, topic, Throwables.getRootCause(throwable).getMessage()); if (throwable instanceof Mqtt3SubAckException) { Logger.debug(((Mqtt3SubAckException) throwable).getMqttMessage()); } + Logger.trace(throwable); } else { getClientDataMap().get(ClientKey.of(client)).addSubscription(MqttTopicFilter.of(topic)); @@ -205,8 +205,7 @@ void mqtt5Publish( client.toAsync().publish(publishMessage).whenComplete((publishResult, throwable) -> { if (throwable != null) { - Logger.error(throwable, - "{} failed PUBLISH to TOPIC '{}': {}", + Logger.error("{} failed PUBLISH to TOPIC '{}': {}", clientLogPrefix, topic, Throwables.getRootCause(throwable).getMessage()); @@ -215,6 +214,7 @@ void mqtt5Publish( } else if (throwable instanceof Mqtt5PubRecException) { Logger.debug(((Mqtt5PubRecException) throwable).getMqttMessage()); } + Logger.trace(throwable); } else { Logger.debug("{} received PUBLISH acknowledgement {}", clientLogPrefix, publishResult); } @@ -245,11 +245,11 @@ void mqtt3Publish( client.toAsync().publish(publishMessage).whenComplete((publishResult, throwable) -> { if (throwable != null) { - Logger.error(throwable, - "{} failed PUBLISH to TOPIC '{}': {}", + Logger.error("{} failed PUBLISH to TOPIC '{}': {}", clientLogPrefix, topic, Throwables.getRootCause(throwable).getMessage()); + Logger.trace(throwable); } else { Logger.debug("{} received PUBLISH acknowledgement {}", clientLogPrefix, publishResult); } @@ -270,14 +270,14 @@ void mqtt5Unsubscribe(final @NotNull Mqtt5Client client, final @NotNull Unsubscr client.toAsync().unsubscribe(unsubscribeMessage).whenComplete((unsubAck, throwable) -> { if (throwable != null) { - Logger.error(throwable, - "{} failed UNSUBSCRIBE from TOPIC '{}': {}", + Logger.error("{} failed UNSUBSCRIBE from TOPIC '{}': {}", clientLogPrefix, topic, Throwables.getRootCause(throwable).getMessage()); if (throwable instanceof Mqtt5UnsubAckException) { Logger.debug(((Mqtt5UnsubAckException) throwable).getMqttMessage()); } + Logger.trace(throwable); } else { getClientDataMap().get(ClientKey.of(client)).removeSubscription(MqttTopicFilter.of(topic)); @@ -298,11 +298,11 @@ void mqtt3Unsubscribe(final @NotNull Mqtt3Client client, final @NotNull Unsubscr client.toAsync().unsubscribe(unsubscribeMessage).whenComplete((unsubAck, throwable) -> { if (throwable != null) { - Logger.error(throwable, - "{} failed UNSUBSCRIBE from TOPIC '{}': {}", + Logger.error("{} failed UNSUBSCRIBE from TOPIC '{}': {}", clientLogPrefix, topic, Throwables.getRootCause(throwable).getMessage()); + Logger.trace(throwable); } else { getClientDataMap().get(ClientKey.of(client)).removeSubscription(MqttTopicFilter.of(topic)); Logger.debug("{} received UNSUBACK", clientLogPrefix); diff --git a/src/main/java/com/hivemq/cli/utils/LoggerUtils.java b/src/main/java/com/hivemq/cli/utils/LoggerUtils.java index 123b83d3a..69db4cf09 100644 --- a/src/main/java/com/hivemq/cli/utils/LoggerUtils.java +++ b/src/main/java/com/hivemq/cli/utils/LoggerUtils.java @@ -118,15 +118,17 @@ public static void logCommandError( final @NotNull String message, final @NotNull Exception exception, final @NotNull DebugOptions debugOptions) { - if (debugOptions.isDebug() || debugOptions.isVerbose()) { - Logger.error(exception, message); + final String exceptionMessage = Throwables.getRootCause(exception).getMessage(); + if (exceptionMessage != null) { + Logger.error("{}. Reason: '{}'", message, exceptionMessage); } else { - final String exceptionMessage = Throwables.getRootCause(exception).getMessage(); - if (exceptionMessage != null) { - Logger.error("{}. Reason: '{}'", message, exceptionMessage); - } else { - Logger.error("{}. Use '-d' option to get more detailed information.", message); - } + Logger.error("{}.", message); + } + if (!debugOptions.isDebug()) { + Logger.error("Use '-d' or 'v' option to get more detailed information."); + } + if (debugOptions.isVerbose()) { + Logger.error(exception); } }