Skip to content

Commit 8fed4c8

Browse files
author
Jean Barkhuysen
committed
Log server errors as well as returning to http.
For easier debugging.
1 parent 50f4fe0 commit 8fed4c8

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

16 MB
Binary file not shown.

server.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,34 @@ type module struct {
3737
}
3838

3939
func (s *server) handleIndex(w http.ResponseWriter, r *http.Request) {
40+
slog.Info("received index request wtih query params ?since=%q and ?limit=%q", r.URL.Query().Get("since"), r.URL.Query().Get("limit"))
4041
var since time.Time
4142
var err error
4243
if sinceParam := r.URL.Query().Get("since"); sinceParam != "" {
4344
since, err = time.Parse(time.RFC3339, sinceParam)
4445
if err != nil {
45-
http.Error(w, fmt.Sprintf("error converting 'since' param %s: %v", sinceParam, err), http.StatusBadRequest)
46+
errStr := fmt.Sprintf("error converting 'since' param %s: %v", sinceParam, err)
47+
slog.Error(errStr)
48+
http.Error(w, errStr, http.StatusBadRequest)
4649
return
4750
}
4851
}
4952

5053
limit := defaultNumberOfOutputs
5154
if limitParam := r.URL.Query().Get("limit"); limitParam != "" {
5255
if limit, err = strconv.ParseInt(limitParam, 10, 64); err != nil {
53-
http.Error(w, fmt.Sprintf("error converting 'limit' param %s: %v", limitParam, err), http.StatusBadRequest)
56+
errStr := fmt.Sprintf("error converting 'limit' param %s: %v", limitParam, err)
57+
slog.Error(errStr)
58+
http.Error(w, errStr, http.StatusBadRequest)
5459
return
5560
}
5661
}
5762

5863
repoTags, err := s.idb.FetchRepoTags(r.Context(), since, limit)
5964
if err != nil {
60-
http.Error(w, fmt.Sprintf("error fetching repo tags: %v", err), http.StatusInternalServerError)
65+
errStr := fmt.Sprintf("error fetching repo tags: %v", err)
66+
slog.Error(errStr)
67+
http.Error(w, errStr, http.StatusInternalServerError)
6168
return
6269
}
6370

@@ -69,15 +76,19 @@ func (s *server) handleIndex(w http.ResponseWriter, r *http.Request) {
6976
Timestamp: rt.Created.Format(time.RFC3339),
7077
})
7178
if err != nil {
72-
http.Error(w, fmt.Sprintf("error marshalling response for %v: %v", rt, err), http.StatusInternalServerError)
79+
errStr := fmt.Sprintf("error marshalling response for %v: %v", rt, err)
80+
slog.Error(errStr)
81+
http.Error(w, errStr, http.StatusInternalServerError)
7382
return
7483
}
7584

7685
lines = append(lines, string(out))
7786
}
7887

7988
if _, err := fmt.Fprint(w, strings.Join(lines, "\n")); err != nil {
80-
http.Error(w, fmt.Sprintf("error writing response: %v", err), http.StatusInternalServerError)
89+
errStr := fmt.Sprintf("error writing response: %v", err)
90+
slog.Error(errStr)
91+
http.Error(w, errStr, http.StatusInternalServerError)
8192
return
8293
}
8394
}

0 commit comments

Comments
 (0)