Skip to content

Commit

Permalink
Catch errors with connection details and prevent errors with internal…
Browse files Browse the repository at this point in the history
… network informations
  • Loading branch information
denniskniep committed Feb 28, 2022
1 parent 7ba0b4b commit 7f8cc6f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
1 change: 1 addition & 0 deletions neo4j-datasource-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- Use official name Neo4j instead of Neo4J
- Use neo4j logo with blue background to support both dark and light theme. Logo was barely visible with the light theme.
- Catch errors with connection details and prevent errors with internal network informations

## [1.1.0-beta]
### Added
Expand Down
20 changes: 18 additions & 2 deletions neo4j-datasource-plugin/pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,11 @@ func checkHealth(dataSourceInstanceSettings *backend.DataSourceInstanceSettings)
settings, err := unmarshalDataSourceSettings(dataSourceInstanceSettings)

if err != nil {
errorMsg := "Can not deserialize DataSource settings"
log.DefaultLogger.Error(errorMsg, err.Error())
return &backend.CheckHealthResult{
Status: backend.HealthStatusError,
Message: err.Error(),
Message: errorMsg,
}, nil
}

Expand All @@ -197,9 +199,23 @@ func checkHealth(dataSourceInstanceSettings *backend.DataSourceInstanceSettings)
_, err = query(settings, neo4JQuery)

if err != nil {
errMsg := "Error occured while connecting to Neo4j!"
log.DefaultLogger.Error(errMsg, err.Error())

switch t := err.(type) {
case *neo4j.ConnectivityError:
errMsg = "ConnectivityError: Can not connect to specified url"
case *neo4j.UsageError:
errMsg = t.Message
case *neo4j.TokenExpiredError:
errMsg = t.Message
case *neo4j.Neo4jError:
errMsg = t.Msg
}

return &backend.CheckHealthResult{
Status: backend.HealthStatusError,
Message: err.Error(),
Message: errMsg,
}, nil
}

Expand Down
24 changes: 19 additions & 5 deletions neo4j-datasource-plugin/pkg/plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ func TestHealthcheckIsErrorDueToInvalidHost(t *testing.T) {
testCheckHealthAndMessage(t, settings, ERROR_STATUS, "ConnectivityError")
}

func TestHealthcheckIsErrorDueToDeserialize(t *testing.T) {
skipIfIsShort(t)

settings := &backend.DataSourceInstanceSettings{}
settings.JSONData = []byte { 1 }

const ERROR_STATUS backend.HealthStatus = 2
testCheckHealthAndMessageWithSettings(t, settings, ERROR_STATUS, "Can not deserialize DataSource settings")
}

func TestHealthcheckIsErrorDueToInvalidPort(t *testing.T) {
skipIfIsShort(t)
settings := neo4JSettings{
Expand All @@ -52,7 +62,7 @@ func TestHealthcheckIsErrorDueToInvalidPort(t *testing.T) {
}

const ERROR_STATUS backend.HealthStatus = 2
testCheckHealthAndMessage(t, settings, ERROR_STATUS, "ConnectivityError")
testCheckHealthAndMessage(t, settings, ERROR_STATUS, "ConnectivityError: Can not connect to specified url")
}

func TestHealthcheckIsErrorDueToInvalidUsername(t *testing.T) {
Expand All @@ -65,7 +75,7 @@ func TestHealthcheckIsErrorDueToInvalidUsername(t *testing.T) {
}

const ERROR_STATUS backend.HealthStatus = 2
testCheckHealthAndMessage(t, settings, ERROR_STATUS, "Unauthorized")
testCheckHealthAndMessage(t, settings, ERROR_STATUS, "unauthorized due to authentication failure")
}

func TestHealthcheckIsErrorDueToInvalidPassword(t *testing.T) {
Expand All @@ -78,7 +88,7 @@ func TestHealthcheckIsErrorDueToInvalidPassword(t *testing.T) {
}

const ERROR_STATUS backend.HealthStatus = 2
testCheckHealthAndMessage(t, settings, ERROR_STATUS, "Unauthorized")
testCheckHealthAndMessage(t, settings, ERROR_STATUS, "unauthorized due to authentication failure")
}

func TestHealthcheckIsErrorDueToNoAuth(t *testing.T) {
Expand All @@ -91,7 +101,7 @@ func TestHealthcheckIsErrorDueToNoAuth(t *testing.T) {
}

const ERROR_STATUS backend.HealthStatus = 2
testCheckHealthAndMessage(t, settings, ERROR_STATUS, "Unauthorized")
testCheckHealthAndMessage(t, settings, ERROR_STATUS, "Unsupported authentication token")
}

func TestHealthcheckIsErrorDueToInvalidDatabase(t *testing.T) {
Expand All @@ -104,7 +114,7 @@ func TestHealthcheckIsErrorDueToInvalidDatabase(t *testing.T) {
}

const ERROR_STATUS backend.HealthStatus = 2
testCheckHealthAndMessage(t, settings, ERROR_STATUS, "DatabaseNotFound")
testCheckHealthAndMessage(t, settings, ERROR_STATUS, "this database does not exist")
}

func testCheckHealth(t *testing.T, neo4JSettings neo4JSettings, expectedStatus backend.HealthStatus) {
Expand All @@ -115,6 +125,10 @@ func testCheckHealthAndMessage(t *testing.T, neo4JSettings neo4JSettings, expect
settings := &backend.DataSourceInstanceSettings{}
settings.JSONData = asJsonBytes(t, neo4JSettings)

testCheckHealthAndMessageWithSettings(t, settings, expectedStatus, expectedMessagePart)
}

func testCheckHealthAndMessageWithSettings(t *testing.T, settings *backend.DataSourceInstanceSettings, expectedStatus backend.HealthStatus, expectedMessagePart string) {
res, err := checkHealth(settings)

if err != nil {
Expand Down

0 comments on commit 7f8cc6f

Please sign in to comment.