Skip to content

Commit

Permalink
refactor(language-web): log less spurious exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
kris7t committed Dec 24, 2024
1 parent b707069 commit 269753f
Showing 1 changed file with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import com.google.gson.GsonBuilder;
import com.google.gson.JsonIOException;
import com.google.gson.JsonParseException;
import org.eclipse.jetty.io.EofException;
import org.eclipse.jetty.websocket.api.Callback;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.api.annotations.*;
import org.eclipse.jetty.websocket.api.exceptions.WebSocketTimeoutException;
import org.eclipse.xtext.resource.IResourceServiceProvider;
import org.eclipse.xtext.web.server.ISession;
import org.slf4j.Logger;
Expand Down Expand Up @@ -83,10 +85,16 @@ public void onClose(int statusCode, String reason) {
@OnWebSocketError
public void onError(Throwable error) {
executor.dispose();
if (webSocketSession == null) {
if (webSocketSession == null || !webSocketSession.isOpen()) {
return;
}
LOG.error("Internal websocket error in connection from " + webSocketSession.getRemoteSocketAddress(), error);
switch (error) {
case WebSocketTimeoutException ignored -> LOG.warn("Websocket connection timed out", error);
case EofException ignored -> LOG.warn("Websocket connection already closed", error);
default ->
LOG.error("Internal websocket error in connection from " + webSocketSession.getRemoteSocketAddress(),
error);
}
}

@OnWebSocketMessage
Expand Down Expand Up @@ -126,16 +134,17 @@ public void onMessage(Reader reader) {

@Override
public void onResponse(XtextWebResponse response) throws ResponseHandlerException {
if (webSocketSession == null) {
if (webSocketSession == null || !webSocketSession.isOpen()) {
throw new ResponseHandlerException("Trying to send message when websocket is disconnected");
}
var responseString = gson.toJson(response);
webSocketSession.sendText(responseString, Callback.from(() -> {}, this::writeFailed));
webSocketSession.sendText(responseString, Callback.from(() -> {
}, this::writeFailed));
}

public void writeFailed(Throwable x) {
if (webSocketSession == null) {
LOG.error("Cannot complete async write to disconnected websocket", x);
if (webSocketSession == null || !webSocketSession.isOpen()) {
LOG.warn("Cannot complete async write to disconnected websocket", x);
return;
}
LOG.warn("Cannot complete async write to websocket " + webSocketSession.getRemoteSocketAddress(), x);
Expand Down

0 comments on commit 269753f

Please sign in to comment.