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

Better error management #299

Merged
merged 6 commits into from Jul 25, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

#### Changed
- (#282) Branch support added. You can now specify your branches with a `#branch` fragment at the end of your git url. E.g.: https://github.com/mesg-foundation/service-ethereum-erc20#websocket
- (#299) Add more user friendly errors when failing to connect to the Core or Docker

#### Added
- (#242) Add more details in command `mesg-core service validate`
Expand Down
22 changes: 0 additions & 22 deletions cmd/utils/confirmable.go

This file was deleted.

32 changes: 31 additions & 1 deletion cmd/utils/error.go
Expand Up @@ -4,13 +4,43 @@ import (
"fmt"
"os"

"github.com/docker/docker/client"
"github.com/logrusorgru/aurora"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

const (
cannotReachTheCore = "Cannot reach the Core"
startCore = "Please start the core by running: mesg-core start"
cannotReachDocker = "Cannot reach Docker"
installDocker = "Please make sure Docker is running.\nIf Docker is not installed on your machine you can install it here: https://store.docker.com/search?type=edition&offering=community"
)

// HandleError display the error and stop the process if error exist
func HandleError(err error) {
if err != nil {
fmt.Println(aurora.Red(err))
fmt.Println(errorMessage(err))
os.Exit(0)
}
}

func errorMessage(err error) string {
switch {
case coreConnectionError(err):
return aurora.Sprintf("%s\n%s", aurora.Red(cannotReachTheCore), startCore)
case dockerDaemonError(err):
return aurora.Sprintf("%s\n%s", aurora.Red(cannotReachDocker), installDocker)
default:
return aurora.Red(err.Error()).String()
}
}

func coreConnectionError(err error) bool {
s := status.Convert(err)
return s.Code() == codes.Unavailable
}

func dockerDaemonError(err error) bool {
return client.IsErrConnectionFailed(err)
}
36 changes: 36 additions & 0 deletions cmd/utils/error_test.go
@@ -0,0 +1,36 @@
package utils

import (
"errors"
"testing"

"github.com/docker/docker/client"
"github.com/stvp/assert"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

var testCoreConnectionErr = status.Error(codes.Unavailable, "test")
var testDockerConnectionErr = client.ErrorConnectionFailed("test")

func TestCoreConnectionError(t *testing.T) {
assert.True(t, coreConnectionError(testCoreConnectionErr))
assert.False(t, coreConnectionError(nil))
assert.False(t, coreConnectionError(errors.New("test")))
}

func TestDockerDaemonError(t *testing.T) {
assert.True(t, dockerDaemonError(testDockerConnectionErr))
assert.False(t, dockerDaemonError(nil))
assert.False(t, dockerDaemonError(errors.New("test")))
}

func TestErrorMessage(t *testing.T) {
assert.Contains(t, cannotReachTheCore, errorMessage(testCoreConnectionErr))
assert.Contains(t, startCore, errorMessage(testCoreConnectionErr))

assert.Contains(t, cannotReachDocker, errorMessage(testDockerConnectionErr))
assert.Contains(t, installDocker, errorMessage(testDockerConnectionErr))

assert.Contains(t, "errorX", errorMessage(errors.New("errorX")))
}