Started work on crash reporting
This commit is contained in:
@@ -105,6 +105,8 @@ If an error status code is returned then the body will be as follows.
|
|||||||
|
|
||||||
`errorCode`'s returned from the main library:
|
`errorCode`'s returned from the main library:
|
||||||
|
|
||||||
|
* misconfigured
|
||||||
|
* Backend is configured incorrectly (such as App returning nil crash table, but key has crash permission)
|
||||||
* invalidKey
|
* invalidKey
|
||||||
* API Key is invalid or does not have the needed permission for the request.
|
* API Key is invalid or does not have the needed permission for the request.
|
||||||
* invalidBody
|
* invalidBody
|
||||||
|
|||||||
@@ -1,59 +1,77 @@
|
|||||||
package darkstorm
|
package darkstorm
|
||||||
|
|
||||||
import "net/http"
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
type ArchivedCrash struct {
|
type ArchivedCrash struct {
|
||||||
Error string
|
Error string `json:"error" bson:"error"`
|
||||||
Stack string
|
Stack string `json:"stack" bson:"stack"`
|
||||||
Platform string
|
Platform string `json:"platform" bson:"platform"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type IndividualCrash struct {
|
type IndividualCrash struct {
|
||||||
Platform string
|
Platform string `json:"platform" bson:"platform"`
|
||||||
Error string
|
Error string `json:"error" bson:"error"`
|
||||||
Stack string
|
Stack string `json:"stack" bson:"stack"`
|
||||||
Count int
|
Count int `json:"count" bson:"count"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CrashReport struct {
|
type CrashReport struct {
|
||||||
ID string
|
ID string `json:"id" bson:"_id"`
|
||||||
Error string
|
Error string `json:"error" bson:"error"`
|
||||||
FirstLine string
|
FirstLine string `json:"firstLine" bson:"firstLine"`
|
||||||
Individual []IndividualCrash
|
Individual []IndividualCrash `json:"individual" bson:"individual"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c CrashReport) GetID() string {
|
func (c CrashReport) GetID() string {
|
||||||
return c.ID
|
return c.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
type crashReq struct {
|
|
||||||
ID string
|
|
||||||
Platform string
|
|
||||||
Error string
|
|
||||||
Stack string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Backend) reportCrash(w http.ResponseWriter, r *http.Request) {
|
func (b *Backend) reportCrash(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var ap App
|
||||||
hdr, err := b.ParseHeader(r)
|
hdr, err := b.ParseHeader(r)
|
||||||
if hdr.k == nil || hdr.k.Perm["crash"] {
|
if hdr.k != nil {
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
ap = b.GetApp(hdr.k)
|
||||||
|
}
|
||||||
|
if ap == nil || hdr.k.Perm["crash"] || errors.Is(err, ErrApiKeyUnauthorized) {
|
||||||
|
ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application not authorized")
|
||||||
|
return
|
||||||
|
} else if err != nil {
|
||||||
|
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
defer r.Body.Close()
|
||||||
//TODO
|
var crash IndividualCrash
|
||||||
|
err = json.NewDecoder(r.Body).Decode(&crash)
|
||||||
|
if err != nil || crash.Platform == "" || crash.Error == "" || crash.Stack == "" {
|
||||||
|
ReturnError(w, http.StatusBadRequest, "invalidBody", "Bad request")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//TODO
|
tab := ap.CrashTable()
|
||||||
|
if tab == nil {
|
||||||
|
ReturnError(w, http.StatusInternalServerError, "misconfigured", "Server misconfigured")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !tab.IsArchived(crash) {
|
||||||
|
err = tab.InsertCrash(crash)
|
||||||
|
if err != nil {
|
||||||
|
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusCreated)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Backend) deleteCrash(w http.ResponseWriter, r *http.Request) {
|
func (b *Backend) deleteCrash(w http.ResponseWriter, r *http.Request) {
|
||||||
hdr, err := b.ParseHeader(r)
|
hdr, err := b.ParseHeader(r)
|
||||||
if hdr.k == nil || hdr.k.Perm["management"] {
|
if hdr.k == nil || hdr.k.Perm["management"] || errors.Is(err, ErrApiKeyUnauthorized) {
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application not authorized")
|
||||||
return
|
return
|
||||||
}
|
} else if err != nil {
|
||||||
if err != nil {
|
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||||
//TODO
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//TODO
|
//TODO
|
||||||
|
|||||||
@@ -21,5 +21,11 @@ type Table[T IDStruct] interface {
|
|||||||
|
|
||||||
type CrashTable interface {
|
type CrashTable interface {
|
||||||
Table[CrashReport]
|
Table[CrashReport]
|
||||||
InsertCrash(ID string, report IndividualCrash) error
|
// Move a crash type to archive. All instances that perfectly match that appear in CrashReport.Individual should be deleted.
|
||||||
|
// If a CrashReport ends up with an empty Individual array it should also be deleted.
|
||||||
|
Archive(ArchivedCrash)
|
||||||
|
IsArchived(IndividualCrash) bool
|
||||||
|
// Add the IndividualCrash report to the crash table. If a CrashReport exists that matches, then it gets added to CrashReport.Individual.
|
||||||
|
// If an IndividualCrash exists that is a perfect match, Count is incremented instead of adding it to the array.
|
||||||
|
InsertCrash(IndividualCrash) error
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -118,6 +118,9 @@ func (b *Backend) CreateUser(w http.ResponseWriter, r *http.Request) {
|
|||||||
if hdr.k == nil || !hdr.k.Perm["user"] || errors.Is(err, ErrApiKeyUnauthorized) {
|
if hdr.k == nil || !hdr.k.Perm["user"] || errors.Is(err, ErrApiKeyUnauthorized) {
|
||||||
ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application not authorized")
|
ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application not authorized")
|
||||||
return
|
return
|
||||||
|
} else if err != nil {
|
||||||
|
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
var req createUserRequest
|
var req createUserRequest
|
||||||
@@ -186,6 +189,9 @@ func (b *Backend) Login(w http.ResponseWriter, r *http.Request) {
|
|||||||
if hdr.k == nil || !hdr.k.Perm["user"] || errors.Is(err, ErrApiKeyUnauthorized) {
|
if hdr.k == nil || !hdr.k.Perm["user"] || errors.Is(err, ErrApiKeyUnauthorized) {
|
||||||
ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application not authorized")
|
ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application not authorized")
|
||||||
return
|
return
|
||||||
|
} else if err != nil {
|
||||||
|
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
var req loginRequest
|
var req loginRequest
|
||||||
|
|||||||
Reference in New Issue
Block a user