Proper context.Context usage

This commit is contained in:
Caleb Gardner
2024-10-24 00:00:08 -05:00
parent fcab9458ee
commit 6965917e76
19 changed files with 126 additions and 118 deletions
+15 -14
View File
@@ -1,6 +1,7 @@
package backend
import (
"context"
"encoding/json"
"log"
"net/http"
@@ -49,7 +50,7 @@ func (b *Backend) reportCrash(w http.ResponseWriter, r *http.Request) {
return
}
if filter, ok := ap.(CrashFilterApp); ok {
if !filter.AddCrash(crash) {
if !filter.ShouldAddCrash(r.Context(), crash) {
return
}
}
@@ -59,8 +60,8 @@ func (b *Backend) reportCrash(w http.ResponseWriter, r *http.Request) {
ReturnError(w, http.StatusInternalServerError, "misconfigured", "Server misconfigured")
return
}
if !tab.IsArchived(crash) {
err = tab.InsertCrash(crash)
if !tab.IsArchived(r.Context(), crash) {
err = tab.InsertCrash(r.Context(), crash)
if err != nil {
log.Println("crash insertion error:", err)
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
@@ -83,7 +84,7 @@ func (b *Backend) deleteCrash(w http.ResponseWriter, r *http.Request) {
ReturnError(w, http.StatusBadRequest, "badRequest", "Bad request")
return
}
b.actualCrashDelete(w, b.GetApp(hdr.Key), crashID)
b.actualCrashDelete(r.Context(), w, b.GetApp(hdr.Key), crashID)
}
func (b *Backend) managementDeleteCrash(w http.ResponseWriter, r *http.Request) {
@@ -105,16 +106,16 @@ func (b *Backend) managementDeleteCrash(w http.ResponseWriter, r *http.Request)
ReturnError(w, http.StatusBadRequest, "badRequest", "Bad request")
return
}
b.actualCrashDelete(w, ap, crashID)
b.actualCrashDelete(r.Context(), w, ap, crashID)
}
func (b *Backend) actualCrashDelete(w http.ResponseWriter, ap App, crashID string) {
func (b *Backend) actualCrashDelete(ctx context.Context, w http.ResponseWriter, ap App, crashID string) {
crash := ap.CrashTable()
if crash == nil {
ReturnError(w, http.StatusInternalServerError, "misconfigured", "Server Misconfigured")
return
}
err := crash.Remove(crashID)
err := crash.Remove(ctx, crashID)
if err != nil && err != ErrNotFound {
log.Println("error when deleting crash:", err)
}
@@ -135,7 +136,7 @@ func (b *Backend) archiveCrash(w http.ResponseWriter, r *http.Request) {
ReturnError(w, http.StatusBadRequest, "invalidBody", "Bad request")
return
}
b.actualCrashArchive(w, b.GetApp(hdr.Key), toArchive)
b.actualCrashArchive(r.Context(), w, b.GetApp(hdr.Key), toArchive)
}
func (b *Backend) managementArchiveCrash(w http.ResponseWriter, r *http.Request) {
@@ -159,22 +160,22 @@ func (b *Backend) managementArchiveCrash(w http.ResponseWriter, r *http.Request)
ReturnError(w, http.StatusBadRequest, "invalidBody", "Bad request")
return
}
b.actualCrashArchive(w, ap, toArchive)
b.actualCrashArchive(r.Context(), w, ap, toArchive)
}
func (b *Backend) actualCrashArchive(w http.ResponseWriter, ap App, toArchive ArchivedCrash) {
func (b *Backend) actualCrashArchive(ctx context.Context, w http.ResponseWriter, ap App, toArchive ArchivedCrash) {
crash := ap.CrashTable()
if crash == nil {
ReturnError(w, http.StatusInternalServerError, "misconfigured", "Server Misconfigured")
return
}
err := crash.Archive(toArchive)
err := crash.Archive(ctx, toArchive)
if err != nil {
log.Println("error archive crash:", err)
return
}
first, _, _ := strings.Cut(toArchive.Stack, "\n")
crashes, err := crash.Find(map[string]any{"error": toArchive.Error, "firstLine": first})
crashes, err := crash.Find(ctx, map[string]any{"error": toArchive.Error, "firstLine": first})
if err == ErrNotFound {
return
} else if err != nil {
@@ -194,12 +195,12 @@ func (b *Backend) actualCrashArchive(w http.ResponseWriter, ap App, toArchive Ar
}
}
if len(c.Individual) == 0 {
err = crash.Remove(c.ID)
err = crash.Remove(ctx, c.ID)
if err != nil {
log.Println("error removing empty crash report:", err)
}
} else if len(c.Individual) < ogLen {
err = crash.PartUpdate(c.ID, map[string]any{"individual": c.Individual})
err = crash.PartUpdate(ctx, c.ID, map[string]any{"individual": c.Individual})
if err != nil {
log.Println("error updating individual crash reports:", err)
}