Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grammes add error context for connection failure #17

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions gremconnect/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package gremconnect

import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"sync"
Expand Down Expand Up @@ -67,22 +69,35 @@ func (ws *WebSocket) Connect() error {
ws.address = ws.address + "/gremlin"
}

ws.conn, _, err = dialer.Dial(ws.address, http.Header{})
var httpResponse *http.Response
ws.conn, httpResponse, err = dialer.Dial(ws.address, http.Header{})
if err != nil {
if httpResponse != nil {
//noinspection GoUnhandledErrorResult
defer httpResponse.Body.Close()

// Try to read the http response to add context to the error
errorOutput, readErr := ioutil.ReadAll(httpResponse.Body)
if readErr == nil {
return fmt.Errorf("error connecting to address. response: %s. error %v", string(errorOutput), err)
}
}

if err == nil {
ws.connected = true
return err
}

handler := func(appData string) error {
ws.Lock()
ws.connected = true
ws.Unlock()
return nil
}
ws.connected = true

ws.conn.SetPongHandler(handler)
handler := func(appData string) error {
ws.Lock()
ws.connected = true
ws.Unlock()
return nil
}

return err
ws.conn.SetPongHandler(handler)

return nil
}

// IsConnected returns whether the given
Expand Down