Skip to content

Commit b711895

Browse files
committed
Use driver-level error structs instead of gobolt provided ones
1 parent be0d343 commit b711895

File tree

5 files changed

+157
-16
lines changed

5 files changed

+157
-16
lines changed

neo4j/error.go

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,72 @@ import (
2424
"github.com/neo4j-drivers/gobolt"
2525
)
2626

27+
type databaseError struct {
28+
classification string
29+
code string
30+
message string
31+
}
32+
33+
type connectorError struct {
34+
state int
35+
code int
36+
description string
37+
}
38+
2739
type driverError struct {
2840
message string
2941
}
3042

31-
func newDriverError(format string, args ...interface{}) error {
43+
func (failure *databaseError) Classification() string {
44+
return failure.classification
45+
}
46+
47+
func (failure *databaseError) Code() string {
48+
return failure.code
49+
}
50+
51+
func (failure *databaseError) Message() string {
52+
return failure.message
53+
}
54+
55+
func (failure *databaseError) Error() string {
56+
return fmt.Sprintf("database returned error [%s]: %s", failure.code, failure.message)
57+
}
58+
59+
func (failure *connectorError) State() int {
60+
return failure.state
61+
}
62+
63+
func (failure *connectorError) Code() int {
64+
return failure.code
65+
}
66+
67+
func (failure *connectorError) Description() string {
68+
return failure.description
69+
}
70+
71+
func (failure *connectorError) Error() string {
72+
return fmt.Sprintf("expected connection to be in READY state, where it is %d [error is %d]", failure.state, failure.code)
73+
}
74+
75+
func (failure *driverError) Error() string {
76+
return failure.message
77+
}
78+
79+
func newDriverError(format string, args ...interface{}) gobolt.GenericError {
3280
return &driverError{message: fmt.Sprintf(format, args...)}
3381
}
3482

35-
func isRetriableError(err error) bool {
36-
return gobolt.IsServiceUnavailable(err) || gobolt.IsTransientError(err) || gobolt.IsWriteError(err)
83+
func newDatabaseError(classification, code, message string) gobolt.DatabaseError {
84+
return &databaseError{code: code, message: message, classification: classification}
3785
}
3886

39-
func (err *driverError) Error() string {
40-
return err.message
87+
func newConnectorError(state int, code int, description string) gobolt.ConnectorError {
88+
return &connectorError{state: state, code: code, description: description}
89+
}
90+
91+
func isRetriableError(err error) bool {
92+
return gobolt.IsServiceUnavailable(err) || gobolt.IsTransientError(err) || gobolt.IsWriteError(err)
4193
}
4294

4395
// IsServiceUnavailable is a utility method to check if the provided error can be classified

neo4j/gobolt_driver.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ func configToGoboltConfig(config *Config) *gobolt.Config {
6262
&dateTimeValueHandler{},
6363
&durationValueHandler{},
6464
},
65+
GenericErrorFactory: newDriverError,
66+
ConnectorErrorFactory: newConnectorError,
67+
DatabaseErrorFactory: newDatabaseError,
6568
}
6669
}
6770

neo4j/test-integration/transaction_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ package test_integration
2121

2222
import (
2323
"fmt"
24+
"github.com/neo4j/neo4j-go-driver/neo4j/test-integration/utils"
2425
"time"
2526

26-
"github.com/neo4j-drivers/gobolt"
2727
"github.com/neo4j/neo4j-go-driver/neo4j"
2828
"github.com/neo4j/neo4j-go-driver/neo4j/test-integration/control"
2929

@@ -63,10 +63,7 @@ var _ = Describe("Transaction", func() {
6363
})
6464

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

7168
It("should work on WriteTransaction", func() {
7269
times := 0
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (c) 2002-2018 "Neo4j,"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
package utils
21+
22+
import (
23+
"fmt"
24+
"github.com/neo4j-drivers/gobolt"
25+
)
26+
27+
type testDatabaseError struct {
28+
classification string
29+
code string
30+
message string
31+
}
32+
33+
type testConnectorError struct {
34+
state int
35+
code int
36+
description string
37+
}
38+
39+
type testDriverError struct {
40+
message string
41+
}
42+
43+
func (failure *testDatabaseError) Classification() string {
44+
return failure.classification
45+
}
46+
47+
func (failure *testDatabaseError) Code() string {
48+
return failure.code
49+
}
50+
51+
func (failure *testDatabaseError) Message() string {
52+
return failure.message
53+
}
54+
55+
func (failure *testDatabaseError) Error() string {
56+
return fmt.Sprintf("database returned error [%s]: %s", failure.code, failure.message)
57+
}
58+
59+
func (failure *testConnectorError) State() int {
60+
return failure.state
61+
}
62+
63+
func (failure *testConnectorError) Code() int {
64+
return failure.code
65+
}
66+
67+
func (failure *testConnectorError) Description() string {
68+
return failure.description
69+
}
70+
71+
func (failure *testConnectorError) Error() string {
72+
return fmt.Sprintf("expected connection to be in READY state, where it is %d [error is %d]", failure.state, failure.code)
73+
}
74+
75+
func (failure *testDriverError) Error() string {
76+
return failure.message
77+
}
78+
79+
func NewDriverErrorForTest(format string, args ...interface{}) gobolt.GenericError {
80+
return &testDriverError{message: fmt.Sprintf(format, args...)}
81+
}
82+
83+
func NewDatabaseErrorForTest(classification, code, message string) gobolt.DatabaseError {
84+
return &testDatabaseError{code: code, message: message, classification: classification}
85+
}
86+
87+
func NewConnectorErrorForTest(state int, code int, description string) gobolt.ConnectorError {
88+
return &testConnectorError{state: state, code: code, description: description}
89+
}

neo4j/utils/test/omega_error_matchers.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ type connectorErrorMatcher struct {
119119
}
120120

121121
func (matcher *databaseErrorMatcher) Match(actual interface{}) (success bool, err error) {
122-
databaseError, ok := actual.(*gobolt.DatabaseError)
122+
databaseError, ok := actual.(gobolt.DatabaseError)
123123
if !ok {
124124
return false, nil
125125
}
@@ -140,7 +140,7 @@ func (matcher *databaseErrorMatcher) Match(actual interface{}) (success bool, er
140140
}
141141

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

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

211211
func (matcher *connectorErrorMatcher) Match(actual interface{}) (success bool, err error) {
212-
connectorError, ok := actual.(*gobolt.ConnectorError)
212+
connectorError, ok := actual.(gobolt.ConnectorError)
213213
if !ok {
214214
return false, nil
215215
}
@@ -230,7 +230,7 @@ func (matcher *connectorErrorMatcher) Match(actual interface{}) (success bool, e
230230
}
231231

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

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

0 commit comments

Comments
 (0)