Skip to content

Commit

Permalink
api: return not found when region doesn't exist (#8869)
Browse files Browse the repository at this point in the history
close #8868

Signed-off-by: lhy1024 <[email protected]>

Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
  • Loading branch information
lhy1024 and ti-chi-bot[bot] authored Dec 25, 2024
1 parent 95bfbe6 commit c2f72ac
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
4 changes: 4 additions & 0 deletions pkg/mcs/scheduling/server/apis/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1476,6 +1476,10 @@ func getRegionByID(c *gin.Context) {
c.String(http.StatusBadRequest, err.Error())
return
}
if regionID == 0 {
c.String(http.StatusBadRequest, errs.ErrRegionInvalidID.FastGenByArgs().Error())
return
}
regionInfo := svr.GetBasicCluster().GetRegion(regionID)
if regionInfo == nil {
c.String(http.StatusNotFound, errs.ErrRegionNotFound.FastGenByArgs(regionID).Error())
Expand Down
12 changes: 12 additions & 0 deletions server/api/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,16 @@ func (h *regionHandler) GetRegionByID(w http.ResponseWriter, r *http.Request) {
h.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}
if regionID == 0 {
h.rd.JSON(w, http.StatusBadRequest, errs.ErrRegionInvalidID.FastGenByArgs())
return
}

regionInfo := rc.GetRegion(regionID)
if regionInfo == nil {
h.rd.JSON(w, http.StatusNotFound, errs.ErrRegionNotFound.FastGenByArgs(regionID).Error())
return
}
b, err := response.MarshalRegionInfoJSON(r.Context(), regionInfo)
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
Expand Down Expand Up @@ -101,6 +109,10 @@ func (h *regionHandler) GetRegion(w http.ResponseWriter, r *http.Request) {
}

regionInfo := rc.GetRegionByKey(paramsByte[0])
if regionInfo == nil {
h.rd.JSON(w, http.StatusNotFound, errs.ErrRegionNotFound.FastGenByArgs().Error())
return
}
b, err := response.MarshalRegionInfoJSON(r.Context(), regionInfo)
if err != nil {
h.rd.JSON(w, http.StatusInternalServerError, err.Error())
Expand Down
8 changes: 7 additions & 1 deletion server/api/region_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ func (suite *regionTestSuite) TestRegion() {
r.UpdateBuckets(buckets, r.GetBuckets())
re := suite.Require()
mustRegionHeartbeat(re, suite.svr, r)
url := fmt.Sprintf("%s/region/id/%d", suite.urlPrefix, r.GetID())
url := fmt.Sprintf("%s/region/id/%d", suite.urlPrefix, 0)
re.NoError(tu.CheckGetJSON(testDialClient, url, nil, tu.Status(re, http.StatusBadRequest)))
url = fmt.Sprintf("%s/region/id/%d", suite.urlPrefix, 2333)
re.NoError(tu.CheckGetJSON(testDialClient, url, nil, tu.Status(re, http.StatusNotFound)))
url = fmt.Sprintf("%s/region/id/%d", suite.urlPrefix, r.GetID())
r1 := &response.RegionInfo{}
r1m := make(map[string]any)
re.NoError(tu.ReadGetJSON(re, testDialClient, url, r1))
Expand All @@ -96,6 +100,8 @@ func (suite *regionTestSuite) TestRegion() {
re.Equal(core.HexRegionKeyStr([]byte("a")), keys[0].(string))
re.Equal(core.HexRegionKeyStr([]byte("b")), keys[1].(string))

url = fmt.Sprintf("%s/region/key/%s", suite.urlPrefix, "c")
re.NoError(tu.CheckGetJSON(testDialClient, url, nil, tu.Status(re, http.StatusNotFound)))
url = fmt.Sprintf("%s/region/key/%s", suite.urlPrefix, "a")
r2 := &response.RegionInfo{}
re.NoError(tu.ReadGetJSON(re, testDialClient, url, r2))
Expand Down
3 changes: 3 additions & 0 deletions tests/integrations/mcs/scheduling/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,9 @@ func (suite *apiTestSuite) checkRegions(cluster *tests.TestCluster) {
err = testutil.ReadGetJSON(re, tests.TestDialClient, urlPrefix, &resp)
re.NoError(err)
re.Equal(3., resp["count"])
urlPrefix = fmt.Sprintf("%s/scheduling/api/v1/regions/0", scheServerAddr)
testutil.CheckGetJSON(tests.TestDialClient, urlPrefix, nil,
testutil.Status(re, http.StatusBadRequest))
urlPrefix = fmt.Sprintf("%s/scheduling/api/v1/regions/233", scheServerAddr)
testutil.CheckGetJSON(tests.TestDialClient, urlPrefix, nil,
testutil.Status(re, http.StatusNotFound), testutil.StringContain(re, "not found"))
Expand Down

0 comments on commit c2f72ac

Please sign in to comment.