Skip to content
This repository was archived by the owner on Feb 21, 2024. It is now read-only.

Commit 8032b1e

Browse files
jaffeecodysoyland
authored andcommitted
use errors.Cause in handler so that we return correct status codes
1 parent 614fa91 commit 8032b1e

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

handler.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ func (h *Handler) handlePostIndex(w http.ResponseWriter, r *http.Request) {
437437
}
438438

439439
_, err = h.API.CreateIndex(r.Context(), indexName, req.Options)
440-
if err == ErrIndexExists {
440+
if errors.Cause(err) == ErrIndexExists {
441441
http.Error(w, err.Error(), http.StatusConflict)
442442
return
443443
} else if err != nil {
@@ -464,7 +464,7 @@ func (h *Handler) handlePostIndexAttrDiff(w http.ResponseWriter, r *http.Request
464464

465465
attrs, err := h.API.IndexAttrDiff(r.Context(), indexName, req.Blocks)
466466
if err != nil {
467-
if err == ErrIndexNotFound {
467+
if errors.Cause(err) == ErrIndexNotFound {
468468
http.Error(w, err.Error(), http.StatusNotFound)
469469
} else {
470470
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -573,7 +573,7 @@ func (h *Handler) handleDeleteFrame(w http.ResponseWriter, r *http.Request) {
573573

574574
err := h.API.DeleteFrame(r.Context(), indexName, frameName)
575575
if err != nil {
576-
if err == ErrIndexNotFound {
576+
if errors.Cause(err) == ErrIndexNotFound {
577577
if err := json.NewEncoder(w).Encode(deleteIndexResponse{}); err != nil {
578578
h.Logger.Printf("response encoding error: %s", err)
579579
}
@@ -612,7 +612,7 @@ func (h *Handler) handlePostFrameField(w http.ResponseWriter, r *http.Request) {
612612
}
613613

614614
if err := h.API.CreateField(r.Context(), indexName, frameName, field); err != nil {
615-
if err == ErrFrameNotFound {
615+
if errors.Cause(err) == ErrFrameNotFound {
616616
http.Error(w, err.Error(), http.StatusNotFound)
617617
} else {
618618
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -641,7 +641,7 @@ func (h *Handler) handleDeleteFrameField(w http.ResponseWriter, r *http.Request)
641641
fieldName := mux.Vars(r)["field"]
642642

643643
if err := h.API.DeleteField(r.Context(), indexName, frameName, fieldName); err != nil {
644-
if err == ErrFrameNotFound {
644+
if errors.Cause(err) == ErrFrameNotFound {
645645
http.Error(w, err.Error(), http.StatusNotFound)
646646
} else {
647647
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -691,7 +691,7 @@ func (h *Handler) handleGetFrameViews(w http.ResponseWriter, r *http.Request) {
691691

692692
views, err := h.API.Views(r.Context(), indexName, frameName)
693693
if err != nil {
694-
if err == ErrFrameNotFound {
694+
if errors.Cause(err) == ErrFrameNotFound {
695695
http.Error(w, err.Error(), http.StatusNotFound)
696696
} else {
697697
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -717,7 +717,7 @@ func (h *Handler) handleDeleteView(w http.ResponseWriter, r *http.Request) {
717717
viewName := mux.Vars(r)["view"]
718718

719719
if err := h.API.DeleteView(r.Context(), indexName, frameName, viewName); err != nil {
720-
if err == ErrFrameNotFound {
720+
if errors.Cause(err) == ErrFrameNotFound {
721721
http.Error(w, err.Error(), http.StatusNotFound)
722722
} else {
723723
http.Error(w, err.Error(), http.StatusBadRequest)
@@ -1051,7 +1051,7 @@ func (h *Handler) handlePostFragmentData(w http.ResponseWriter, r *http.Request)
10511051
}
10521052

10531053
if err = h.API.UnmarshalFragment(r.Context(), q.Get("index"), q.Get("frame"), q.Get("view"), slice, r.Body); err != nil {
1054-
if err == ErrFrameNotFound {
1054+
if errors.Cause(err) == ErrFrameNotFound {
10551055
http.Error(w, ErrFrameNotFound.Error(), http.StatusNotFound)
10561056
} else {
10571057
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -1065,7 +1065,7 @@ func (h *Handler) handleGetFragmentBlockData(w http.ResponseWriter, r *http.Requ
10651065
if err != nil {
10661066
if _, ok := err.(BadRequestError); ok {
10671067
http.Error(w, err.Error(), http.StatusBadRequest)
1068-
} else if err == ErrFragmentNotFound {
1068+
} else if errors.Cause(err) == ErrFragmentNotFound {
10691069
http.Error(w, err.Error(), http.StatusNotFound)
10701070
} else {
10711071
http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -1091,7 +1091,7 @@ func (h *Handler) handleGetFragmentBlocks(w http.ResponseWriter, r *http.Request
10911091

10921092
blocks, err := h.API.FragmentBlocks(r.Context(), q.Get("index"), q.Get("frame"), q.Get("view"), slice)
10931093
if err != nil {
1094-
if err == ErrFragmentNotFound {
1094+
if errors.Cause(err) == ErrFragmentNotFound {
10951095
http.Error(w, err.Error(), http.StatusNotFound)
10961096
} else {
10971097
http.Error(w, err.Error(), http.StatusInternalServerError)

0 commit comments

Comments
 (0)