From e43409923ce0e3b7eba0f5f28812eafaef11e12e Mon Sep 17 00:00:00 2001 From: Caleb Gardner Date: Tue, 9 Jul 2024 20:39:26 -0500 Subject: [PATCH] Added crash filtering Added version field to individual crash --- internal/backend/README.md | 2 ++ internal/backend/app.go | 6 ++++++ internal/backend/crash.go | 18 ++++++++++++------ internal/backend/db/valkey.go | 2 +- internal/blog/main.go | 1 - 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/internal/backend/README.md b/internal/backend/README.md index cffe407..051c8ef 100644 --- a/internal/backend/README.md +++ b/internal/backend/README.md @@ -61,6 +61,7 @@ Users are stored per backend and not per app. { count: 1, // We do not store duplicates. If a duplicate does occur platform: "android", + appVersion: "v1.0.0", // Application version error: "error", stack: "stacktrace" } @@ -279,6 +280,7 @@ Request Body: { id: "UUID", // This is an ignored value, but it is highly recommended to include it to prevent reporting the same crash multiple times. platform: "android", + appVersion: "v1.0.0", error: "error", stack: "stacktrace" } diff --git a/internal/backend/app.go b/internal/backend/app.go index 0a3d5bf..e305468 100644 --- a/internal/backend/app.go +++ b/internal/backend/app.go @@ -9,6 +9,12 @@ type App interface { CrashTable() CrashTable } +// Allows for an App to filter crashes before they get added to the DB, such as making sure the crash is from the correct version. +type CrashFilterApp interface { + App + AddCrash(IndividualCrash) bool +} + type ExtendedApp interface { // Extension is called for any calls to /{appID}/ // Alternatively, use Backend.HandleFunc for more customizability diff --git a/internal/backend/crash.go b/internal/backend/crash.go index 536e210..672abfc 100644 --- a/internal/backend/crash.go +++ b/internal/backend/crash.go @@ -14,10 +14,11 @@ type ArchivedCrash struct { } type IndividualCrash struct { - Platform string `json:"platform" bson:"platform"` - Error string `json:"error" bson:"error"` - Stack string `json:"stack" bson:"stack"` - Count int `json:"count" bson:"count"` + Platform string `json:"platform" bson:"platform"` + AppVersion string `json:"appVersion" bson:"appVersion"` + Error string `json:"error" bson:"error"` + Stack string `json:"stack" bson:"stack"` + Count int `json:"count" bson:"count"` } type CrashReport struct { @@ -43,10 +44,15 @@ func (b *Backend) reportCrash(w http.ResponseWriter, r *http.Request) { defer r.Body.Close() var crash IndividualCrash err = json.NewDecoder(r.Body).Decode(&crash) - if err != nil || crash.Platform == "" || crash.Error == "" || crash.Stack == "" { + if err != nil || crash.Platform == "" || crash.AppVersion == "" || crash.Error == "" || crash.Stack == "" { ReturnError(w, http.StatusBadRequest, "invalidBody", "Bad request") return } + if filter, ok := ap.(CrashFilterApp); ok { + if !filter.AddCrash(crash) { + return + } + } tab := ap.CrashTable() if tab == nil { log.Printf("key %v has crash permission, but app does not have a crash table", hdr.Key.AppID) @@ -60,8 +66,8 @@ func (b *Backend) reportCrash(w http.ResponseWriter, r *http.Request) { ReturnError(w, http.StatusInternalServerError, "internal", "Server error") return } + w.WriteHeader(http.StatusCreated) } - w.WriteHeader(http.StatusCreated) } func (b *Backend) deleteCrash(w http.ResponseWriter, r *http.Request) { diff --git a/internal/backend/db/valkey.go b/internal/backend/db/valkey.go index 9666e43..f884c7b 100644 --- a/internal/backend/db/valkey.go +++ b/internal/backend/db/valkey.go @@ -2,7 +2,7 @@ package db /* TODO -Currently there isn't a clean way to implement this (as far as I can tell). +Currently there isn't a easy and clean way to implement this (as far as I can tell). valkey-go relies on an internal library for it's command builder, which makes it impossible to use properly for generics without manually writing out the Index command. I could probably do this, but it's a pain. diff --git a/internal/blog/main.go b/internal/blog/main.go index 6355b6c..05966a0 100644 --- a/internal/blog/main.go +++ b/internal/blog/main.go @@ -33,7 +33,6 @@ func NewBlogApp(b *backend.Backend, db *mongo.Database, mux *http.ServeMux) *Blo mux.HandleFunc("GET /author/{authorID}", out.reqAuthorInfo) mux.HandleFunc("POST /author", out.addAuthorInfo) mux.HandleFunc("POST /author/{authorID}", out.updateAuthorInfo) - //TODO return out }