Skip to content

Commit ea2371e

Browse files
committed
coderd: Handle http response errors in parsePagination
1 parent 55028d2 commit ea2371e

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-
func parsePagination(r *http.Request) (p codersdk.Pagination, err error) {
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+
func parsePagination(w http.ResponseWriter, r *http.Request) (p codersdk.Pagination, ok bool) {
1417
var (
1518
afterID = uuid.Nil
1619
limit = -1 // Default to no limit and return all results.
1720
offset = 0
1821
)
1922

23+
var err error
2024
if s := r.URL.Query().Get("after_id"); s != "" {
2125
afterID, err = uuid.Parse(r.URL.Query().Get("after_id"))
2226
if err != nil {
23-
return p, 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+
return p, false
2431
}
2532
}
2633
if s := r.URL.Query().Get("limit"); s != "" {
2734
limit, err = strconv.Atoi(s)
2835
if err != nil {
29-
return p, 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+
return p, false
3040
}
3141
}
3242
if s := r.URL.Query().Get("offset"); s != "" {
3343
offset, err = strconv.Atoi(s)
3444
if err != nil {
35-
return p, 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+
return p, 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-
if err != 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-
if err != 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)