|
1 | 1 | package coderd
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
4 | 5 | "net/http"
|
5 | 6 | "strconv"
|
6 | 7 |
|
7 | 8 | "github.com/google/uuid"
|
8 |
| - "golang.org/x/xerrors" |
9 | 9 |
|
| 10 | + "github.com/coder/coder/coderd/httpapi" |
10 | 11 | "github.com/coder/coder/codersdk"
|
11 | 12 | )
|
12 | 13 |
|
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) { |
14 | 17 | var (
|
15 | 18 | afterID = uuid.Nil
|
16 | 19 | limit = -1 // Default to no limit and return all results.
|
17 | 20 | offset = 0
|
18 | 21 | )
|
19 | 22 |
|
| 23 | + var err error |
20 | 24 | if s := r.URL.Query().Get("after_id"); s != "" {
|
21 | 25 | afterID, err = uuid.Parse(r.URL.Query().Get("after_id"))
|
22 | 26 | 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 |
24 | 31 | }
|
25 | 32 | }
|
26 | 33 | if s := r.URL.Query().Get("limit"); s != "" {
|
27 | 34 | limit, err = strconv.Atoi(s)
|
28 | 35 | 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 |
30 | 40 | }
|
31 | 41 | }
|
32 | 42 | if s := r.URL.Query().Get("offset"); s != "" {
|
33 | 43 | offset, err = strconv.Atoi(s)
|
34 | 44 | 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 |
36 | 49 | }
|
37 | 50 | }
|
38 | 51 |
|
39 | 52 | return codersdk.Pagination{
|
40 | 53 | AfterID: afterID,
|
41 | 54 | Limit: limit,
|
42 | 55 | Offset: offset,
|
43 |
| - }, nil |
| 56 | + }, true |
44 | 57 | }
|
0 commit comments