Catch exceptions from StreamEvents JSON Serialization
Sometimes GSONs JSON serialization method would fail, allowing the exception to travel up to the event dispatcher and deregister the listening user, without any logs identicating an issue. The user would still be connected over SSH, not knowing something went wrong. Catch the exception, log it, and allow the stream-event to continue serving events. Change-Id: I4b1d4db6934566f1213ebc2e416dd95c1b0b07e6
This commit is contained in:
@@ -40,6 +40,8 @@ import com.google.inject.Inject;
|
||||
|
||||
import org.apache.sshd.server.Environment;
|
||||
import org.kohsuke.args4j.Option;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
@@ -51,6 +53,9 @@ import java.util.concurrent.LinkedBlockingQueue;
|
||||
@RequiresCapability(GlobalCapability.STREAM_EVENTS)
|
||||
@CommandMetaData(name = "stream-events", description = "Monitor events occurring in real time")
|
||||
final class StreamEvents extends BaseCommand {
|
||||
private static final Logger log =
|
||||
LoggerFactory.getLogger(StreamEvents.class);
|
||||
|
||||
/** Maximum number of events that may be queued up for each connection. */
|
||||
private static final int MAX_EVENTS = 128;
|
||||
|
||||
@@ -262,9 +267,16 @@ final class StreamEvents extends BaseCommand {
|
||||
}
|
||||
|
||||
private void write(final Object message) {
|
||||
final String msg = gson.toJson(message) + "\n";
|
||||
synchronized (stdout) {
|
||||
stdout.print(msg);
|
||||
String msg = null;
|
||||
try {
|
||||
msg = gson.toJson(message) + "\n";
|
||||
} catch (Exception e) {
|
||||
log.warn("Could not deserialize the msg: ", e);
|
||||
}
|
||||
if (msg != null) {
|
||||
synchronized (stdout) {
|
||||
stdout.print(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user