-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patherrors.go
51 lines (47 loc) · 1.33 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package neoql
import (
"gopkg.in/neoql.v1/types"
"gopkg.in/packstream.v1"
)
const (
// unauthorizedCode is the error code returned by Neo4j when authentication failed.
unauthorizedCode = "Neo.ClientError.Security.Unauthorized"
)
// getMessageError reads the packstream Structure, and returns an error if the message signature is a failure.
func getMessageError(res *packstream.Structure) error {
if res.Signature != byteFailure {
return nil
}
if len(res.Fields) == 0 {
return types.ErrProtocol
} else if m, isMap := res.Fields[0].(map[string]interface{}); !isMap {
return types.ErrProtocol
} else {
err := types.CypherError{}
if code, codeExist := m["code"]; codeExist {
if str, ok := code.(string); ok {
err.Code = str
}
}
if message, messageExist := m["message"]; messageExist {
if str, ok := message.(string); ok {
err.Message = str
}
}
if err.Code == "" && err.Message == "" {
return types.ErrProtocol
}
if err.Code == unauthorizedCode {
return types.ErrUnauthorized
}
return &err
}
}
// messageError always returns an error by reading the "res" structure. If the structure does not contains any valid
// error, it returns the "defaultErr".
func messageError(res *packstream.Structure, defaultErr error) error {
if err := getMessageError(res); err != nil {
return err
}
return defaultErr
}