Skip to content

Commit

Permalink
Actually ignore WS errors (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
XDoubleU authored Nov 5, 2024
1 parent 1a29c71 commit 5023ebe
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions pkg/communication/ws/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ func ErrorResponse(
errorDto := errortools.NewErrorDto(status, message)
err := wsjson.Write(ctx, conn, errorDto)
if err != nil {
if canIgnoreError(err) {
return
}

contexttools.Logger(ctx).
ErrorContext(ctx, "failed to write JSON", logging.ErrAttr(err))
}
Expand All @@ -36,6 +32,10 @@ func ErrorResponse(
// ServerErrorResponse is used to handle
// internal server errors that occurred on a WebSocket.
func ServerErrorResponse(ctx context.Context, conn *websocket.Conn, err error) {
if isCloseError(err) {
return
}

contexttools.Logger(ctx).
ErrorContext(ctx, "server error occurred", logging.ErrAttr(err))

Expand All @@ -50,7 +50,7 @@ func ServerErrorResponse(ctx context.Context, conn *websocket.Conn, err error) {
// UpgradeErrorResponse is used to handle an error that
// occurred during the upgrade towards a WebSocket.
func UpgradeErrorResponse(w http.ResponseWriter, r *http.Request, err error) {
if !canIgnoreError(err) {
if !isWSProtocolViolation(err) {
contexttools.Logger(r.Context()).
ErrorContext(r.Context(), "WS upgrade error occurred", logging.ErrAttr(err))
}
Expand All @@ -74,10 +74,13 @@ func ForbiddenResponse(ctx context.Context, conn *websocket.Conn) {
ErrorResponse(ctx, conn, http.StatusForbidden, errortools.MessageForbidden)
}

func canIgnoreError(err error) bool {
// EOF, close errors and WS Protocol Violations can be ignored
func isWSProtocolViolation(err error) bool {
return strings.Contains(err.Error(), "WebSocket protocol violation")
}

func isCloseError(err error) bool {
// EOF, close errors
var closeError websocket.CloseError
return errors.Is(err, io.EOF) ||
errors.As(err, &closeError) ||
strings.Contains(err.Error(), "WebSocket protocol violation")
errors.As(err, &closeError)
}

0 comments on commit 5023ebe

Please sign in to comment.