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

Refactor connect to return custom error #228

Merged
merged 2 commits into from Apr 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion client/connection/manager.go
Expand Up @@ -20,6 +20,8 @@ var (
ErrNoConnection = errors.New("no connection exists")
// ErrAlreadyExists error indicates that aciton applieto to manager expects no active connection (i.e. connect)
ErrAlreadyExists = errors.New("connection already exists")
// ErrConnectionCancelled indicates that connection in progress was cancelled by request of api user
ErrConnectionCancelled = errors.New("connection was cancelled")
// ErrOpenvpnProcessDied indicates that Connect method didn't reach "Connected" phase due to openvpn error
ErrOpenvpnProcessDied = errors.New("openvpn process died")
)
Expand Down Expand Up @@ -60,7 +62,11 @@ func (manager *connectionManager) Connect(consumerID, providerID identity.Identi
}
}()

return manager.startConnection(consumerID, providerID)
err = manager.startConnection(consumerID, providerID)
if err == utils.ErrRequestCancelled {
return ErrConnectionCancelled
}
return err
}

func (manager *connectionManager) startConnection(consumerID, providerID identity.Identity) (err error) {
Expand Down
3 changes: 1 addition & 2 deletions client/connection/manager_test.go
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/mysterium/node/server"
"github.com/mysterium/node/service_discovery/dto"
"github.com/mysterium/node/session"
"github.com/mysterium/node/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"sync"
Expand Down Expand Up @@ -209,7 +208,7 @@ func (tc *testContext) TestConnectingInProgressCanBeCanceled() {

connectWaiter.Wait()

assert.Equal(tc.T(), utils.ErrRequestCancelled, err)
assert.Equal(tc.T(), ErrConnectionCancelled, err)
}

func (tc *testContext) TestConnectMethodReturnsErrorIfOpenvpnClientExitsDuringConnect() {
Expand Down
5 changes: 2 additions & 3 deletions tequilapi/endpoints/connection.go
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/mysterium/node/openvpn/middlewares/client/bytescount"
"github.com/mysterium/node/tequilapi/utils"
"github.com/mysterium/node/tequilapi/validation"
node_utils "github.com/mysterium/node/utils"
"net/http"
)

Expand Down Expand Up @@ -72,8 +71,8 @@ func (ce *ConnectionEndpoint) Create(resp http.ResponseWriter, req *http.Request
switch err {
case connection.ErrAlreadyExists:
utils.SendError(resp, err, http.StatusConflict)
case node_utils.ErrRequestCancelled:
utils.SendErrorMessage(resp, "connection was cancelled", statusConnectCancelled)
case connection.ErrConnectionCancelled:
utils.SendError(resp, err, statusConnectCancelled)
default:
log.Error(connectionLogPrefix, err)
utils.SendError(resp, err, http.StatusInternalServerError)
Expand Down
2 changes: 1 addition & 1 deletion tequilapi/endpoints/connection_test.go
Expand Up @@ -414,7 +414,7 @@ func TestDisconnectReturnsConflictStatusIfConnectionDoesNotExist(t *testing.T) {

func TestConnectReturnsConnectCancelledStatusWhenErrConnectionCancelledIsEncountered(t *testing.T) {
manager := fakeManager{}
manager.onConnectReturn = utils.ErrRequestCancelled
manager.onConnectReturn = connection.ErrConnectionCancelled

connectionEndpoint := NewConnectionEndpoint(&manager, nil, nil)
req := httptest.NewRequest(
Expand Down