Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit47ba197

Browse files
committed
coderd: Handle http response errors in parsePagination
1 parentd92fec3 commit47ba197

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

‎coderd/pagination.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,57 @@
11
package coderd
22

33
import (
4+
"fmt"
45
"net/http"
56
"strconv"
67

78
"github.com/google/uuid"
8-
"golang.org/x/xerrors"
99

10+
"github.com/coder/coder/coderd/httpapi"
1011
"github.com/coder/coder/codersdk"
1112
)
1213

13-
funcparsePagination(r*http.Request) (p codersdk.Pagination,errerror) {
14+
// parsePagination extracts pagination query params from the http request.
15+
// If an error is encountered, the error is written to w and ok is set to false.
16+
funcparsePagination(w http.ResponseWriter,r*http.Request) (p codersdk.Pagination,okbool) {
1417
var (
1518
afterID=uuid.Nil
1619
limit=-1// Default to no limit and return all results.
1720
offset=0
1821
)
1922

23+
varerrerror
2024
ifs:=r.URL.Query().Get("after_id");s!="" {
2125
afterID,err=uuid.Parse(r.URL.Query().Get("after_id"))
2226
iferr!=nil {
23-
returnp,xerrors.Errorf("after_id must be a valid uuid: %w",err.Error())
27+
httpapi.Write(w,http.StatusBadRequest, httpapi.Response{
28+
Message:fmt.Sprintf("after_id must be a valid uuid: %s",err.Error()),
29+
})
30+
returnp,false
2431
}
2532
}
2633
ifs:=r.URL.Query().Get("limit");s!="" {
2734
limit,err=strconv.Atoi(s)
2835
iferr!=nil {
29-
returnp,xerrors.Errorf("limit must be an integer: %w",err.Error())
36+
httpapi.Write(w,http.StatusBadRequest, httpapi.Response{
37+
Message:fmt.Sprintf("limit must be an integer: %s",err.Error()),
38+
})
39+
returnp,false
3040
}
3141
}
3242
ifs:=r.URL.Query().Get("offset");s!="" {
3343
offset,err=strconv.Atoi(s)
3444
iferr!=nil {
35-
returnp,xerrors.Errorf("offset must be an integer: %w",err.Error())
45+
httpapi.Write(w,http.StatusBadRequest, httpapi.Response{
46+
Message:fmt.Sprintf("offset must be an integer: %s",err.Error()),
47+
})
48+
returnp,false
3649
}
3750
}
3851

3952
return codersdk.Pagination{
4053
AfterID:afterID,
4154
Limit:limit,
4255
Offset:offset,
43-
},nil
56+
},true
4457
}

‎coderd/templates.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,8 @@ func (api *api) deleteTemplate(rw http.ResponseWriter, r *http.Request) {
7575
func (api*api)templateVersionsByTemplate(rw http.ResponseWriter,r*http.Request) {
7676
template:=httpmw.TemplateParam(r)
7777

78-
paginationParams,err:=parsePagination(r)
79-
iferr!=nil {
80-
httpapi.Write(rw,http.StatusBadRequest, httpapi.Response{Message:fmt.Sprintf("parse pagination request: %s",err.Error())})
78+
paginationParams,ok:=parsePagination(rw,r)
79+
if!ok {
8180
return
8281
}
8382

‎coderd/users.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,8 @@ func (api *api) users(rw http.ResponseWriter, r *http.Request) {
109109
statusFilter=r.URL.Query().Get("status")
110110
)
111111

112-
paginationParams,err:=parsePagination(r)
113-
iferr!=nil {
114-
httpapi.Write(rw,http.StatusBadRequest, httpapi.Response{Message:fmt.Sprintf("parse pagination request: %s",err.Error())})
112+
paginationParams,ok:=parsePagination(rw,r)
113+
if!ok {
115114
return
116115
}
117116

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp