Skip to content

Commit

Permalink
Log errors encountered in HTTP handlers. (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwhittaker authored Sep 27, 2023
1 parent f4c30e5 commit 4eca79e
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 14 deletions.
10 changes: 8 additions & 2 deletions 04/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,30 @@ func run(ctx context.Context, a *app) error {
http.NotFound(w, r)
return
}
fmt.Fprint(w, indexHtml)
if _, err := fmt.Fprint(w, indexHtml); err != nil {
a.Logger(r.Context()).Error("error writing index.html", "err", err)
}
})
http.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) {
// Search for the list of matching emojis.
query := r.URL.Query().Get("q")
emojis, err := a.searcher.Get().Search(r.Context(), query)
if err != nil {
a.Logger(r.Context()).Error("error getting search results", "err", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// JSON serialize the results.
bytes, err := json.Marshal(emojis)
if err != nil {
a.Logger(r.Context()).Error("error marshaling search results", "err", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintln(w, string(bytes))
if _, err := fmt.Fprintln(w, string(bytes)); err != nil {
a.Logger(r.Context()).Error("error writing search results", "err", err)
}
})
return http.Serve(a.lis, nil)
}
10 changes: 8 additions & 2 deletions 05/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,30 @@ func run(ctx context.Context, a *app) error {
http.NotFound(w, r)
return
}
fmt.Fprint(w, indexHtml)
if _, err := fmt.Fprint(w, indexHtml); err != nil {
a.Logger(r.Context()).Error("error writing index.html", "err", err)
}
})
http.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) {
// Search for the list of matching emojis.
query := r.URL.Query().Get("q")
emojis, err := a.searcher.Get().Search(r.Context(), query)
if err != nil {
a.Logger(r.Context()).Error("error getting search results", "err", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// JSON serialize the results.
bytes, err := json.Marshal(emojis)
if err != nil {
a.Logger(r.Context()).Error("error marshaling search results", "err", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintln(w, string(bytes))
if _, err := fmt.Fprintln(w, string(bytes)); err != nil {
a.Logger(r.Context()).Error("error writing search results", "err", err)
}
})
return http.Serve(a.lis, nil)
}
10 changes: 8 additions & 2 deletions 06/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,30 @@ func run(ctx context.Context, a *app) error {
http.NotFound(w, r)
return
}
fmt.Fprint(w, indexHtml)
if _, err := fmt.Fprint(w, indexHtml); err != nil {
a.Logger(r.Context()).Error("error writing index.html", "err", err)
}
})
http.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) {
// Search for the list of matching emojis.
query := r.URL.Query().Get("q")
emojis, err := a.searcher.Get().Search(r.Context(), query)
if err != nil {
a.Logger(r.Context()).Error("error getting search results", "err", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// JSON serialize the results.
bytes, err := json.Marshal(emojis)
if err != nil {
a.Logger(r.Context()).Error("error marshaling search results", "err", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintln(w, string(bytes))
if _, err := fmt.Fprintln(w, string(bytes)); err != nil {
a.Logger(r.Context()).Error("error writing search results", "err", err)
}
})
return http.Serve(a.lis, nil)
}
10 changes: 8 additions & 2 deletions 07/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,30 @@ func run(ctx context.Context, a *app) error {
http.NotFound(w, r)
return
}
fmt.Fprint(w, indexHtml)
if _, err := fmt.Fprint(w, indexHtml); err != nil {
a.Logger(r.Context()).Error("error writing index.html", "err", err)
}
})
http.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) {
// Search for the list of matching emojis.
query := r.URL.Query().Get("q")
emojis, err := a.searcher.Get().Search(r.Context(), query)
if err != nil {
a.Logger(r.Context()).Error("error getting search results", "err", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// JSON serialize the results.
bytes, err := json.Marshal(emojis)
if err != nil {
a.Logger(r.Context()).Error("error marshaling search results", "err", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintln(w, string(bytes))
if _, err := fmt.Fprintln(w, string(bytes)); err != nil {
a.Logger(r.Context()).Error("error writing search results", "err", err)
}
})
return http.Serve(a.lis, nil)
}
10 changes: 8 additions & 2 deletions 08/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,30 @@ func run(ctx context.Context, a *app) error {
http.NotFound(w, r)
return
}
fmt.Fprint(w, indexHtml)
if _, err := fmt.Fprint(w, indexHtml); err != nil {
a.Logger(r.Context()).Error("error writing index.html", "err", err)
}
})
http.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) {
// Search for the list of matching emojis.
query := r.URL.Query().Get("q")
emojis, err := a.searcher.Get().Search(r.Context(), query)
if err != nil {
a.Logger(r.Context()).Error("error getting search results", "err", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// JSON serialize the results.
bytes, err := json.Marshal(emojis)
if err != nil {
a.Logger(r.Context()).Error("error marshaling search results", "err", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintln(w, string(bytes))
if _, err := fmt.Fprintln(w, string(bytes)); err != nil {
a.Logger(r.Context()).Error("error writing search results", "err", err)
}
})
return http.Serve(a.lis, nil)
}
10 changes: 8 additions & 2 deletions 09/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,30 @@ func run(ctx context.Context, a *app) error {
http.NotFound(w, r)
return
}
fmt.Fprint(w, indexHtml)
if _, err := fmt.Fprint(w, indexHtml); err != nil {
a.Logger(r.Context()).Error("error writing index.html", "err", err)
}
})
http.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) {
// Search for the list of matching emojis.
query := r.URL.Query().Get("q")
emojis, err := a.searcher.Get().Search(r.Context(), query)
if err != nil {
a.Logger(r.Context()).Error("error getting search results", "err", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// JSON serialize the results.
bytes, err := json.Marshal(emojis)
if err != nil {
a.Logger(r.Context()).Error("error marshaling search results", "err", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintln(w, string(bytes))
if _, err := fmt.Fprintln(w, string(bytes)); err != nil {
a.Logger(r.Context()).Error("error writing search results", "err", err)
}
})
return http.Serve(a.lis, nil)
}
10 changes: 8 additions & 2 deletions 10/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ func run(ctx context.Context, a *app) error {
http.NotFound(w, r)
return
}
fmt.Fprint(w, indexHtml)
if _, err := fmt.Fprint(w, indexHtml); err != nil {
a.Logger(ctx).Error("error writing index.html", "err", err)
}
})
http.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) {
a.handleSearch(a.searcher.Get().Search, w, r)
Expand All @@ -67,15 +69,19 @@ func (a *app) handleSearch(search func(context.Context, string) ([]string, error
query := r.URL.Query().Get("q")
emojis, err := search(r.Context(), query)
if err != nil {
a.Logger(r.Context()).Error("error getting search results", "err", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// JSON serialize the results.
bytes, err := json.Marshal(emojis)
if err != nil {
a.Logger(r.Context()).Error("error marshaling search results", "err", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintln(w, string(bytes))
if _, err := fmt.Fprintln(w, string(bytes)); err != nil {
a.Logger(r.Context()).Error("error writing search results", "err", err)
}
}

0 comments on commit 4eca79e

Please sign in to comment.