Skip to content
This repository was archived by the owner on Mar 26, 2020. It is now read-only.

Commit 7436b73

Browse files
authored
Merge pull request #869 from Madhu-1/err_cleanup
code cleanup , added function to return HTTP status code based on error
2 parents 78011ed + edcbd97 commit 7436b73

File tree

27 files changed

+193
-381
lines changed

27 files changed

+193
-381
lines changed

glusterd2/commands/peers/editpeer.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,8 @@ func editPeer(w http.ResponseWriter, r *http.Request) {
4242

4343
txn, err := transaction.NewTxnWithLocks(ctx, peerID)
4444
if err != nil {
45-
if err == transaction.ErrLockTimeout {
46-
restutils.SendHTTPError(ctx, w, http.StatusConflict, err)
47-
} else {
48-
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err)
49-
}
45+
status, err := restutils.ErrToStatusCode(err)
46+
restutils.SendHTTPError(ctx, w, status, err)
5047
return
5148
}
5249
defer txn.Done()

glusterd2/commands/snapshot/snapshot-activate.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,8 @@ func snapshotActivateHandler(w http.ResponseWriter, r *http.Request) {
156156

157157
txn, err := transaction.NewTxnWithLocks(ctx, snapname)
158158
if err != nil {
159-
if err == transaction.ErrLockTimeout {
160-
restutils.SendHTTPError(ctx, w, http.StatusConflict, err)
161-
} else {
162-
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err)
163-
}
159+
status, err := restutils.ErrToStatusCode(err)
160+
restutils.SendHTTPError(ctx, w, status, err)
164161
return
165162
}
166163
defer txn.Done()

glusterd2/commands/snapshot/snapshot-create.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -656,22 +656,16 @@ func snapshotCreateHandler(w http.ResponseWriter, r *http.Request) {
656656

657657
txn, err := transaction.NewTxnWithLocks(ctx, req.VolName, req.SnapName)
658658
if err != nil {
659-
if err == transaction.ErrLockTimeout {
660-
restutils.SendHTTPError(ctx, w, http.StatusConflict, err)
661-
} else {
662-
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err)
663-
}
659+
status, err := restutils.ErrToStatusCode(err)
660+
restutils.SendHTTPError(ctx, w, status, err)
664661
return
665662
}
666663
defer txn.Done()
667664

668665
vol, e := volume.GetVolume(req.VolName)
669666
if e != nil {
670-
if e == gderrors.ErrVolNotFound {
671-
restutils.SendHTTPError(ctx, w, http.StatusNotFound, gderrors.ErrVolNotFound)
672-
} else {
673-
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, e)
674-
}
667+
status, err := restutils.ErrToStatusCode(e)
668+
restutils.SendHTTPError(ctx, w, status, err)
675669
return
676670
}
677671

@@ -720,11 +714,8 @@ func snapshotCreateHandler(w http.ResponseWriter, r *http.Request) {
720714
err = txn.Do()
721715
if err != nil {
722716
logger.WithError(err).Error("snapshot create transaction failed")
723-
if err == transaction.ErrLockTimeout {
724-
restutils.SendHTTPError(ctx, w, http.StatusConflict, err)
725-
} else {
726-
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err)
727-
}
717+
status, err := restutils.ErrToStatusCode(err)
718+
restutils.SendHTTPError(ctx, w, status, err)
728719
return
729720
}
730721

glusterd2/commands/snapshot/snapshot-deactivate.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,8 @@ func snapshotDeactivateHandler(w http.ResponseWriter, r *http.Request) {
144144

145145
txn, err := transaction.NewTxnWithLocks(ctx, snapname)
146146
if err != nil {
147-
if err == transaction.ErrLockTimeout {
148-
restutils.SendHTTPError(ctx, w, http.StatusConflict, err)
149-
} else {
150-
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err)
151-
}
147+
status, err := restutils.ErrToStatusCode(err)
148+
restutils.SendHTTPError(ctx, w, status, err)
152149
return
153150
}
154151
defer txn.Done()

glusterd2/commands/volumes/bricks-status.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/gluster/glusterd2/glusterd2/transaction"
1010
"github.com/gluster/glusterd2/glusterd2/volume"
1111
"github.com/gluster/glusterd2/pkg/api"
12-
"github.com/gluster/glusterd2/pkg/errors"
1312
"github.com/gorilla/mux"
1413
)
1514

@@ -71,11 +70,8 @@ func volumeBricksStatusHandler(w http.ResponseWriter, r *http.Request) {
7170
volname := mux.Vars(r)["volname"]
7271
vol, err := volume.GetVolume(volname)
7372
if err != nil {
74-
if err == errors.ErrVolNotFound {
75-
restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound)
76-
} else {
77-
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err)
78-
}
73+
status, err := restutils.ErrToStatusCode(err)
74+
restutils.SendHTTPError(ctx, w, status, err)
7975
return
8076
}
8177

glusterd2/commands/volumes/volfiles.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package volumecommands
22

33
import (
4-
"fmt"
54
"net/http"
65

76
restutils "github.com/gluster/glusterd2/glusterd2/servers/rest/utils"
87
volgen "github.com/gluster/glusterd2/glusterd2/volgen2"
9-
"github.com/gluster/glusterd2/pkg/errors"
108

119
"github.com/gorilla/mux"
1210
)
@@ -45,11 +43,8 @@ func volfileGetHandler(w http.ResponseWriter, r *http.Request) {
4543
volfile, err := volgen.GetVolfile(volfileid)
4644

4745
if err != nil {
48-
if err == errors.ErrVolFileNotFound {
49-
restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolFileNotFound)
50-
} else {
51-
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, fmt.Sprintf("unable to fetch volfile content %s", volfile))
52-
}
46+
status, err := restutils.ErrToStatusCode(err)
47+
restutils.SendHTTPError(ctx, w, status, err)
5348
return
5449
}
5550

glusterd2/commands/volumes/volume-create.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,8 @@ func volumeCreateHandler(w http.ResponseWriter, r *http.Request) {
117117

118118
txn, err := transaction.NewTxnWithLocks(ctx, req.Name)
119119
if err != nil {
120-
if err == transaction.ErrLockTimeout {
121-
restutils.SendHTTPError(ctx, w, http.StatusConflict, err)
122-
} else {
123-
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err)
124-
}
120+
status, err := restutils.ErrToStatusCode(err)
121+
restutils.SendHTTPError(ctx, w, status, err)
125122
return
126123
}
127124
defer txn.Done()
@@ -153,11 +150,8 @@ func volumeCreateHandler(w http.ResponseWriter, r *http.Request) {
153150
}
154151

155152
if err := txn.Do(); err != nil {
156-
if err == transaction.ErrLockTimeout {
157-
restutils.SendHTTPError(ctx, w, http.StatusConflict, err)
158-
} else {
159-
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err)
160-
}
153+
status, err := restutils.ErrToStatusCode(err)
154+
restutils.SendHTTPError(ctx, w, status, err)
161155
return
162156
}
163157

glusterd2/commands/volumes/volume-delete.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/gluster/glusterd2/glusterd2/transaction"
1010
"github.com/gluster/glusterd2/glusterd2/volgen"
1111
"github.com/gluster/glusterd2/glusterd2/volume"
12-
"github.com/gluster/glusterd2/pkg/errors"
1312

1413
"github.com/gorilla/mux"
1514
"github.com/pborman/uuid"
@@ -68,22 +67,16 @@ func volumeDeleteHandler(w http.ResponseWriter, r *http.Request) {
6867

6968
txn, err := transaction.NewTxnWithLocks(ctx, volname)
7069
if err != nil {
71-
if err == transaction.ErrLockTimeout {
72-
restutils.SendHTTPError(ctx, w, http.StatusConflict, err)
73-
} else {
74-
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err)
75-
}
70+
status, err := restutils.ErrToStatusCode(err)
71+
restutils.SendHTTPError(ctx, w, status, err)
7672
return
7773
}
7874
defer txn.Done()
7975

8076
volinfo, err := volume.GetVolume(volname)
8177
if err != nil {
82-
if err == errors.ErrVolNotFound {
83-
restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound)
84-
} else {
85-
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err)
86-
}
78+
status, err := restutils.ErrToStatusCode(err)
79+
restutils.SendHTTPError(ctx, w, status, err)
8780
return
8881
}
8982

glusterd2/commands/volumes/volume-edit.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,18 @@ func volumeEditHandler(w http.ResponseWriter, r *http.Request) {
3131
//Lock on Volume Name
3232
txn, err := transaction.NewTxnWithLocks(ctx, volname)
3333
if err != nil {
34-
if err == transaction.ErrLockTimeout {
35-
restutils.SendHTTPError(ctx, w, http.StatusConflict, err)
36-
} else {
37-
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err)
38-
}
34+
status, err := restutils.ErrToStatusCode(err)
35+
restutils.SendHTTPError(ctx, w, status, err)
3936
return
4037
}
38+
4139
defer txn.Done()
4240

4341
//validate volume name
4442
volinfo, err := volume.GetVolume(volname)
4543
if err != nil {
46-
if err == errors.ErrVolNotFound {
47-
restutils.SendHTTPError(ctx, w, http.StatusNotFound, errors.ErrVolNotFound)
48-
} else {
49-
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err)
50-
}
44+
status, err := restutils.ErrToStatusCode(err)
45+
restutils.SendHTTPError(ctx, w, status, err)
5146
return
5247
}
5348

glusterd2/commands/volumes/volume-expand.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,8 @@ func volumeExpandHandler(w http.ResponseWriter, r *http.Request) {
7070

7171
txn, err := transaction.NewTxnWithLocks(ctx, volname)
7272
if err != nil {
73-
if err == transaction.ErrLockTimeout {
74-
restutils.SendHTTPError(ctx, w, http.StatusConflict, err)
75-
} else {
76-
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err)
77-
}
73+
status, err := restutils.ErrToStatusCode(err)
74+
restutils.SendHTTPError(ctx, w, status, err)
7875
return
7976
}
8077
defer txn.Done()
@@ -157,11 +154,8 @@ func volumeExpandHandler(w http.ResponseWriter, r *http.Request) {
157154

158155
if err = txn.Do(); err != nil {
159156
logger.WithError(err).WithField("volume-name", volname).Error("volume expand transaction failed")
160-
if err == transaction.ErrLockTimeout {
161-
restutils.SendHTTPError(ctx, w, http.StatusConflict, err)
162-
} else {
163-
restutils.SendHTTPError(ctx, w, http.StatusInternalServerError, err)
164-
}
157+
status, err := restutils.ErrToStatusCode(err)
158+
restutils.SendHTTPError(ctx, w, status, err)
165159
return
166160
}
167161

0 commit comments

Comments
 (0)