This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
forked from schwartzmx/gremtune
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy patherror.go
92 lines (77 loc) · 2.94 KB
/
error.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package gremcos
import (
"errors"
"fmt"
"net"
"github.com/supplyon/gremcos/interfaces"
)
type ErrorCategory string
const (
ErrorCategoryGeneral ErrorCategory = "GeneralErr"
ErrorCategoryConnectivity ErrorCategory = "ConnectivityErr"
ErrorCategoryAuth ErrorCategory = "AuthErr"
ErrorCategoryClient ErrorCategory = "ClientErr"
ErrorCategoryServer ErrorCategory = "ServerErr"
)
type Error struct {
Wrapped error
Category ErrorCategory
}
func (e Error) Error() string {
return fmt.Sprintf("[%s] %v", e.Category, e.Wrapped)
}
var ErrNoConnection = Error{Wrapped: fmt.Errorf("no connection"), Category: ErrorCategoryConnectivity}
// IsNetworkErr determines whether the given error is related to any network issues (timeout, connectivity,..)
func IsNetworkErr(err error) bool {
if errors.Is(err, ErrNoConnection) {
return true
}
errConn := Error{}
if errors.As(err, &errConn) {
return errConn.Category == ErrorCategoryConnectivity
}
if isNetError(err) {
return true
}
return false
}
// isNetError checks if the given error is (or is a wrapped) net.Error
func isNetError(err error) bool {
if err == nil {
return false
}
// call unwrap and try to cast to net.Error as long as possible
for {
if _, ok := err.(net.Error); ok {
return true
}
if err = errors.Unwrap(err); err == nil {
return false
}
}
}
// DetectError detects any possible errors in responses from Gremlin Server and generates an error for each code
func extractError(r interfaces.Response) error {
switch r.Status.Code {
case interfaces.StatusSuccess, interfaces.StatusNoContent, interfaces.StatusPartialContent:
return nil
case interfaces.StatusUnauthorized:
return Error{Wrapped: fmt.Errorf("unauthorized: %s", r.Status.Message), Category: ErrorCategoryAuth}
case interfaces.StatusAuthenticate:
return Error{Wrapped: fmt.Errorf("not authenticated: %s", r.Status.Message), Category: ErrorCategoryAuth}
case interfaces.StatusMalformedRequest:
return Error{Wrapped: fmt.Errorf("malformed request: %s", r.Status.Message), Category: ErrorCategoryClient}
case interfaces.StatusInvalidRequestArguments:
return Error{Wrapped: fmt.Errorf("invalid request arguments: %s", r.Status.Message), Category: ErrorCategoryClient}
case interfaces.StatusServerError:
return Error{Wrapped: fmt.Errorf("server error: %s", r.Status.Message), Category: ErrorCategoryServer}
case interfaces.StatusScriptEvaluationError:
return Error{Wrapped: fmt.Errorf("script evaluation failed: %s", r.Status.Message), Category: ErrorCategoryClient}
case interfaces.StatusServerTimeout:
return Error{Wrapped: fmt.Errorf("server timeout: %s", r.Status.Message), Category: ErrorCategoryServer}
case interfaces.StatusServerSerializationError:
return Error{Wrapped: fmt.Errorf("script evaluation failed: %s", r.Status.Message), Category: ErrorCategoryClient}
default:
return Error{Wrapped: fmt.Errorf("unknown error: %s", r.Status.Message), Category: ErrorCategoryGeneral}
}
}