Skip to content

Commit f3a9751

Browse files
authored
Merge pull request #261 from arangodb/refactor/const-arango-errrors
Use const errors instead of numbers
2 parents 9dac4c7 + d2c93e9 commit f3a9751

File tree

9 files changed

+63
-30
lines changed

9 files changed

+63
-30
lines changed

agency/agency_health.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ package agency
2525
import (
2626
"context"
2727
"fmt"
28+
"net/http"
2829
"net/url"
2930
"strings"
3031
"sync"
@@ -84,7 +85,7 @@ func AreAgentsHealthy(ctx context.Context, clients []driver.Connection) error {
8485
statuses[i].LeaderEndpoint = strings.Join(c.Endpoints(), ",")
8586
statuses[i].IsResponding = true
8687
} else {
87-
if driver.IsArangoErrorWithCode(err, 307) && resp != nil {
88+
if driver.IsArangoErrorWithCode(err, http.StatusTemporaryRedirect) && resp != nil {
8889
location := resp.Header("Location")
8990
// Valid response from a follower
9091
statuses[i].IsLeader = false

client_admin_backup.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ import (
2929

3030
// BackupMeta provides meta data of a backup
3131
type BackupMeta struct {
32-
ID BackupID `json:"id,omitempty"`
33-
Version string `json:"version,omitempty"`
34-
DateTime time.Time `json:"datetime,omitempty"`
35-
NumberOfFiles uint `json:"nrFiles,omitempty"`
36-
NumberOfDBServers uint `json:"nrDBServers,omitempty"`
37-
SizeInBytes uint64 `json:"sizeInBytes,omitempty"`
38-
PotentiallyInconsistent bool `json:"potentiallyInconsistent,omitempty"`
39-
Available bool `json:"available,omitempty"`
40-
NumberOfPiecesPresent uint `json:"nrPiecesPresent,omitempty"`
41-
Keys []BackupMetaSha256 `json:"keys,omitempty"`
32+
ID BackupID `json:"id,omitempty"`
33+
Version string `json:"version,omitempty"`
34+
DateTime time.Time `json:"datetime,omitempty"`
35+
NumberOfFiles uint `json:"nrFiles,omitempty"`
36+
NumberOfDBServers uint `json:"nrDBServers,omitempty"`
37+
SizeInBytes uint64 `json:"sizeInBytes,omitempty"`
38+
PotentiallyInconsistent bool `json:"potentiallyInconsistent,omitempty"`
39+
Available bool `json:"available,omitempty"`
40+
NumberOfPiecesPresent uint `json:"nrPiecesPresent,omitempty"`
41+
Keys []BackupMetaSha256 `json:"keys,omitempty"`
4242
}
4343

4444
// BackupMetaSha256 backup sha details

client_impl.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ func (c *client) SynchronizeEndpoints2(ctx context.Context, dbname string) error
8585
cep, err := c.clusterEndpoints(ctx, dbname)
8686
if err != nil {
8787
// ignore Forbidden: automatic failover is not enabled errors
88-
if !IsArangoErrorWithErrorNum(err, 403, 501, 0, 9, 11) { // 3.2 returns no error code, thus check for 0
88+
if !IsArangoErrorWithErrorNum(err, ErrHttpForbidden, ErrHttpInternal, 0, ErrNotImplemented, ErrForbidden) {
89+
// 3.2 returns no error code, thus check for 0
8990
// 501 with ErrorNum 9 is in there since 3.7, earlier versions returned 403 and ErrorNum 11.
9091
return WithStack(err)
9192
}

cluster/cluster.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ func NewConnection(config ConnectionConfig, connectionBuilder ServerConnectionBu
7373
}
7474

7575
const (
76-
defaultTimeout = 9 * time.Minute
77-
keyEndpoint driver.ContextKey = "arangodb-endpoint"
76+
defaultTimeout = 9 * time.Minute
77+
keyEndpoint driver.ContextKey = "arangodb-endpoint"
7878
)
7979

8080
type clusterConnection struct {

error.go

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,33 @@ import (
3131
"os"
3232
)
3333

34+
const (
35+
// general errors
36+
ErrNotImplemented = 9
37+
ErrForbidden = 11
38+
ErrDisabled = 36
39+
40+
// HTTP error status codes
41+
ErrHttpForbidden = 403
42+
ErrHttpInternal = 501
43+
44+
// Internal ArangoDB storage errors
45+
ErrArangoReadOnly = 1004
46+
47+
// General ArangoDB storage errors
48+
ErrArangoConflict = 1200
49+
ErrArangoDocumentNotFound = 1202
50+
ErrArangoDataSourceNotFound = 1203
51+
ErrArangoUniqueConstraintViolated = 1210
52+
53+
// ArangoDB cluster errors
54+
ErrClusterLeadershipChallengeOngoing = 1495
55+
ErrClusterNotLeader = 1496
56+
57+
// User management errors
58+
ErrUserDuplicate = 1702
59+
)
60+
3461
// ArangoError is a Go error with arangodb specific error information.
3562
type ArangoError struct {
3663
HasError bool `json:"error"`
@@ -95,42 +122,46 @@ func IsArangoErrorWithErrorNum(err error, errorNum ...int) bool {
95122

96123
// IsInvalidRequest returns true if the given error is an ArangoError with code 400, indicating an invalid request.
97124
func IsInvalidRequest(err error) bool {
98-
return IsArangoErrorWithCode(err, 400)
125+
return IsArangoErrorWithCode(err, http.StatusBadRequest)
126+
99127
}
100128

101129
// IsUnauthorized returns true if the given error is an ArangoError with code 401, indicating an unauthorized request.
102130
func IsUnauthorized(err error) bool {
103-
return IsArangoErrorWithCode(err, 401)
131+
return IsArangoErrorWithCode(err, http.StatusUnauthorized)
104132
}
105133

106134
// IsForbidden returns true if the given error is an ArangoError with code 403, indicating a forbidden request.
107135
func IsForbidden(err error) bool {
108-
return IsArangoErrorWithCode(err, 403)
136+
return IsArangoErrorWithCode(err, http.StatusForbidden)
109137
}
110138

111139
// IsNotFound returns true if the given error is an ArangoError with code 404, indicating a object not found.
112140
func IsNotFound(err error) bool {
113-
return IsArangoErrorWithCode(err, 404) || IsArangoErrorWithErrorNum(err, 1202, 1203)
141+
return IsArangoErrorWithCode(err, http.StatusNotFound) ||
142+
IsArangoErrorWithErrorNum(err, ErrArangoDocumentNotFound, ErrArangoDataSourceNotFound)
114143
}
115144

116145
// IsConflict returns true if the given error is an ArangoError with code 409, indicating a conflict.
117146
func IsConflict(err error) bool {
118-
return IsArangoErrorWithCode(err, 409) || IsArangoErrorWithErrorNum(err, 1702)
147+
return IsArangoErrorWithCode(err, http.StatusConflict) || IsArangoErrorWithErrorNum(err, ErrUserDuplicate)
119148
}
120149

121150
// IsPreconditionFailed returns true if the given error is an ArangoError with code 412, indicating a failed precondition.
122151
func IsPreconditionFailed(err error) bool {
123-
return IsArangoErrorWithCode(err, 412) || IsArangoErrorWithErrorNum(err, 1200, 1210)
152+
return IsArangoErrorWithCode(err, http.StatusPreconditionFailed) ||
153+
IsArangoErrorWithErrorNum(err, ErrArangoConflict, ErrArangoUniqueConstraintViolated)
124154
}
125155

126156
// IsNoLeader returns true if the given error is an ArangoError with code 503 error number 1496.
127157
func IsNoLeader(err error) bool {
128-
return IsArangoErrorWithCode(err, 503) && IsArangoErrorWithErrorNum(err, 1496)
158+
return IsArangoErrorWithCode(err, http.StatusServiceUnavailable) && IsArangoErrorWithErrorNum(err, ErrClusterNotLeader)
129159
}
130160

131161
// IsNoLeaderOrOngoing return true if the given error is an ArangoError with code 503 and error number 1496 or 1495
132162
func IsNoLeaderOrOngoing(err error) bool {
133-
return IsArangoErrorWithCode(err, 503) && (IsArangoErrorWithErrorNum(err, 1495) || IsArangoErrorWithErrorNum(err, 1496))
163+
return IsArangoErrorWithCode(err, http.StatusServiceUnavailable) &&
164+
IsArangoErrorWithErrorNum(err, ErrClusterLeadershipChallengeOngoing, ErrClusterNotLeader)
134165
}
135166

136167
// InvalidArgumentError is returned when a go function argument is invalid.

test/database_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434
)
3535

3636
// databaseName is helper to create database name in non-colliding way
37-
func databaseName(parts ... string) string {
37+
func databaseName(parts ...string) string {
3838
return fmt.Sprintf("%s_%s", strings.Join(parts, "_"), uniuri.NewLen(8))
3939
}
4040

test/graph_creation_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ func Test_Graph_AdvancedCreate(t *testing.T) {
137137
})
138138
}
139139

140-
141140
// Test_Graph_AdvancedCreate_Defaults will check if graph created have properly set replication factor
142141
// and write concern by default
143142
func Test_Graph_AdvancedCreate_Defaults(t *testing.T) {

test/server_statistics_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
func checkEnabled(t *testing.T, c driver.Client, ctx context.Context) {
3636
_, err := c.Statistics(ctx)
3737
if err != nil {
38-
if driver.IsArangoErrorWithErrorNum(err, 36) {
38+
if driver.IsArangoErrorWithErrorNum(err, driver.ErrDisabled) {
3939
t.Skip("Statistics disabled.")
4040
}
4141
t.Fatalf("Statistics failed: %s", describe(err))

test/view_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ package test
2525
import (
2626
"context"
2727
"fmt"
28+
"net/http"
2829
"testing"
2930

3031
driver "github.com/arangodb/go-driver"
@@ -577,7 +578,7 @@ func TestArangoSearchPrimarySort(t *testing.T) {
577578
}{
578579
{
579580
Name: "NoneSet",
580-
ErrorCode: 400, // Bad Parameter
581+
ErrorCode: http.StatusBadRequest, // Bad Parameter
581582
},
582583
{
583584
Name: "AscTrue",
@@ -603,25 +604,25 @@ func TestArangoSearchPrimarySort(t *testing.T) {
603604
Name: "SetBothAsc",
604605
InDirection: &directionAsc,
605606
InAscending: &boolTrue,
606-
ErrorCode: 400,
607+
ErrorCode: http.StatusBadRequest,
607608
},
608609
{
609610
Name: "SetBothDesc",
610611
InDirection: &directionDesc,
611612
InAscending: &boolFalse,
612-
ErrorCode: 400,
613+
ErrorCode: http.StatusBadRequest,
613614
},
614615
{
615616
Name: "DirAscAscFalse",
616617
InDirection: &directionAsc,
617618
InAscending: &boolTrue,
618-
ErrorCode: 400,
619+
ErrorCode: http.StatusBadRequest,
619620
},
620621
{
621622
Name: "DirDescAscTrue",
622623
InDirection: &directionAsc,
623624
InAscending: &boolTrue,
624-
ErrorCode: 400,
625+
ErrorCode: http.StatusBadRequest,
625626
},
626627
}
627628

0 commit comments

Comments
 (0)