Skip to content

Commit

Permalink
Use driver-level error structs instead of gobolt provided ones
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-ince committed Oct 11, 2018
1 parent be0d343 commit b711895
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 16 deletions.
62 changes: 57 additions & 5 deletions neo4j/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,72 @@ import (
"github.com/neo4j-drivers/gobolt"
)

type databaseError struct {
classification string
code string
message string
}

type connectorError struct {
state int
code int
description string
}

type driverError struct {
message string
}

func newDriverError(format string, args ...interface{}) error {
func (failure *databaseError) Classification() string {
return failure.classification
}

func (failure *databaseError) Code() string {
return failure.code
}

func (failure *databaseError) Message() string {
return failure.message
}

func (failure *databaseError) Error() string {
return fmt.Sprintf("database returned error [%s]: %s", failure.code, failure.message)
}

func (failure *connectorError) State() int {
return failure.state
}

func (failure *connectorError) Code() int {
return failure.code
}

func (failure *connectorError) Description() string {
return failure.description
}

func (failure *connectorError) Error() string {
return fmt.Sprintf("expected connection to be in READY state, where it is %d [error is %d]", failure.state, failure.code)
}

func (failure *driverError) Error() string {
return failure.message
}

func newDriverError(format string, args ...interface{}) gobolt.GenericError {
return &driverError{message: fmt.Sprintf(format, args...)}
}

func isRetriableError(err error) bool {
return gobolt.IsServiceUnavailable(err) || gobolt.IsTransientError(err) || gobolt.IsWriteError(err)
func newDatabaseError(classification, code, message string) gobolt.DatabaseError {
return &databaseError{code: code, message: message, classification: classification}
}

func (err *driverError) Error() string {
return err.message
func newConnectorError(state int, code int, description string) gobolt.ConnectorError {
return &connectorError{state: state, code: code, description: description}
}

func isRetriableError(err error) bool {
return gobolt.IsServiceUnavailable(err) || gobolt.IsTransientError(err) || gobolt.IsWriteError(err)
}

// IsServiceUnavailable is a utility method to check if the provided error can be classified
Expand Down
3 changes: 3 additions & 0 deletions neo4j/gobolt_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ func configToGoboltConfig(config *Config) *gobolt.Config {
&dateTimeValueHandler{},
&durationValueHandler{},
},
GenericErrorFactory: newDriverError,
ConnectorErrorFactory: newConnectorError,
DatabaseErrorFactory: newDatabaseError,
}
}

Expand Down
7 changes: 2 additions & 5 deletions neo4j/test-integration/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ package test_integration

import (
"fmt"
"github.com/neo4j/neo4j-go-driver/neo4j/test-integration/utils"
"time"

"github.com/neo4j-drivers/gobolt"
"github.com/neo4j/neo4j-go-driver/neo4j"
"github.com/neo4j/neo4j-go-driver/neo4j/test-integration/control"

Expand Down Expand Up @@ -63,10 +63,7 @@ var _ = Describe("Transaction", func() {
})

Context("Retry Mechanism", func() {
transientError := gobolt.NewDatabaseError(map[string]interface{}{
"code": "Neo.TransientError.Transaction.Outdated",
"message": "some transient error",
})
transientError := utils.NewDatabaseErrorForTest("TransientError", "Neo.TransientError.Transaction.Outdated", "some transient error")

It("should work on WriteTransaction", func() {
times := 0
Expand Down
89 changes: 89 additions & 0 deletions neo4j/test-integration/utils/error_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package utils

import (
"fmt"
"github.com/neo4j-drivers/gobolt"
)

type testDatabaseError struct {
classification string
code string
message string
}

type testConnectorError struct {
state int
code int
description string
}

type testDriverError struct {
message string
}

func (failure *testDatabaseError) Classification() string {
return failure.classification
}

func (failure *testDatabaseError) Code() string {
return failure.code
}

func (failure *testDatabaseError) Message() string {
return failure.message
}

func (failure *testDatabaseError) Error() string {
return fmt.Sprintf("database returned error [%s]: %s", failure.code, failure.message)
}

func (failure *testConnectorError) State() int {
return failure.state
}

func (failure *testConnectorError) Code() int {
return failure.code
}

func (failure *testConnectorError) Description() string {
return failure.description
}

func (failure *testConnectorError) Error() string {
return fmt.Sprintf("expected connection to be in READY state, where it is %d [error is %d]", failure.state, failure.code)
}

func (failure *testDriverError) Error() string {
return failure.message
}

func NewDriverErrorForTest(format string, args ...interface{}) gobolt.GenericError {
return &testDriverError{message: fmt.Sprintf(format, args...)}
}

func NewDatabaseErrorForTest(classification, code, message string) gobolt.DatabaseError {
return &testDatabaseError{code: code, message: message, classification: classification}
}

func NewConnectorErrorForTest(state int, code int, description string) gobolt.ConnectorError {
return &testConnectorError{state: state, code: code, description: description}
}
12 changes: 6 additions & 6 deletions neo4j/utils/test/omega_error_matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ type connectorErrorMatcher struct {
}

func (matcher *databaseErrorMatcher) Match(actual interface{}) (success bool, err error) {
databaseError, ok := actual.(*gobolt.DatabaseError)
databaseError, ok := actual.(gobolt.DatabaseError)
if !ok {
return false, nil
}
Expand All @@ -140,7 +140,7 @@ func (matcher *databaseErrorMatcher) Match(actual interface{}) (success bool, er
}

func (matcher *databaseErrorMatcher) FailureMessage(actual interface{}) (message string) {
databaseError, ok := actual.(*gobolt.DatabaseError)
databaseError, ok := actual.(gobolt.DatabaseError)
if !ok {
return fmt.Sprintf("Expected\n\t%#v\nto be a DatabaseError", actual)
}
Expand All @@ -161,7 +161,7 @@ func (matcher *databaseErrorMatcher) FailureMessage(actual interface{}) (message
}

func (matcher *databaseErrorMatcher) NegatedFailureMessage(actual interface{}) (message string) {
databaseError, ok := actual.(*gobolt.DatabaseError)
databaseError, ok := actual.(gobolt.DatabaseError)
if !ok {
return fmt.Sprintf("Expected\n\t%#v\nnot to be a DatabaseError", actual)
}
Expand Down Expand Up @@ -209,7 +209,7 @@ func (matcher *serviceUnavailableErrorMatcher) NegatedFailureMessage(actual inte
}

func (matcher *connectorErrorMatcher) Match(actual interface{}) (success bool, err error) {
connectorError, ok := actual.(*gobolt.ConnectorError)
connectorError, ok := actual.(gobolt.ConnectorError)
if !ok {
return false, nil
}
Expand All @@ -230,7 +230,7 @@ func (matcher *connectorErrorMatcher) Match(actual interface{}) (success bool, e
}

func (matcher *connectorErrorMatcher) FailureMessage(actual interface{}) (message string) {
connectorError, ok := actual.(*gobolt.ConnectorError)
connectorError, ok := actual.(gobolt.ConnectorError)
if !ok {
return fmt.Sprintf("Expected\n\t%#v\nto be a ConnectorError", actual)
}
Expand All @@ -251,7 +251,7 @@ func (matcher *connectorErrorMatcher) FailureMessage(actual interface{}) (messag
}

func (matcher *connectorErrorMatcher) NegatedFailureMessage(actual interface{}) (message string) {
connectorError, ok := actual.(*gobolt.ConnectorError)
connectorError, ok := actual.(gobolt.ConnectorError)
if !ok {
return fmt.Sprintf("Expected\n\t%#v\nnot to be a ConnectorError", actual)
}
Expand Down

0 comments on commit b711895

Please sign in to comment.