Logging
This commit is contained in:
@@ -172,6 +172,7 @@ func (b *Backend) actualCrashArchive(w http.ResponseWriter, ap App, toArchive Ar
|
||||
if err == ErrNotFound {
|
||||
return
|
||||
} else if err != nil {
|
||||
log.Println("error finding matching crashes:", err)
|
||||
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -68,8 +68,12 @@ func (m *MongoTable[T]) PartUpdate(ID string, update map[string]any) error {
|
||||
return res.Err()
|
||||
}
|
||||
|
||||
func (m *MongoTable[CountLog]) RemoveOldLogs(date int) {
|
||||
m.col.DeleteMany(context.Background(), bson.M{"date": bson.M{"$lt": date}})
|
||||
func (m *MongoTable[CountLog]) RemoveOldLogs(date int) error {
|
||||
_, err := m.col.DeleteMany(context.Background(), bson.M{"date": bson.M{"$lt": date}})
|
||||
if err == mongo.ErrNoDocuments {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
func (m *MongoTable[CountLog]) Count(platform string) (int, error) {
|
||||
var filter bson.M
|
||||
|
||||
@@ -85,7 +85,7 @@ func (b *Backend) ParseHeader(r *http.Request) (*ParsedHeader, error) {
|
||||
func (b *Backend) VerifyHeader(w http.ResponseWriter, r *http.Request, keyPerm string, allowManagementKey bool) (*ParsedHeader, error) {
|
||||
hdr, err := b.ParseHeader(r)
|
||||
if hdr == nil || hdr.Key == nil {
|
||||
if err != ErrApiKeyUnauthorized {
|
||||
if err == ErrApiKeyUnauthorized {
|
||||
ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application not authorized")
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -68,6 +68,7 @@ func (b *Backend) countLog(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
err = count.PartUpdate(req.ID, map[string]any{"date": curDate})
|
||||
if err != nil {
|
||||
log.Println("error updating count log:", err)
|
||||
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||
return
|
||||
}
|
||||
@@ -122,6 +123,7 @@ func (b *Backend) getCount(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
out, err := count.Count(r.URL.Query().Get("platform"))
|
||||
if err != nil {
|
||||
log.Println("error getting count:", err)
|
||||
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -141,6 +141,7 @@ func (b *Backend) createUser(w http.ResponseWriter, r *http.Request) {
|
||||
defer b.userMutex.Unlock()
|
||||
matchUsername, err := b.userTable.Find(map[string]any{"username": req.Username})
|
||||
if err != nil && !errors.Is(err, ErrNotFound) {
|
||||
log.Println("error when checking for username collisions:", err)
|
||||
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||
return
|
||||
} else if (err == nil || errors.Is(err, ErrNotFound)) && len(matchUsername) > 0 {
|
||||
@@ -149,6 +150,7 @@ func (b *Backend) createUser(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
matchEmail, err := b.userTable.Find(map[string]any{"email": req.Email})
|
||||
if err != nil && !errors.Is(err, ErrNotFound) {
|
||||
log.Println("error when checking for email collisions:", err)
|
||||
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||
return
|
||||
} else if (err == nil || errors.Is(err, ErrNotFound)) && len(matchEmail) > 0 {
|
||||
@@ -157,11 +159,13 @@ func (b *Backend) createUser(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
u, err := NewUser(req.Username, req.Password, req.Email)
|
||||
if err != nil {
|
||||
log.Println("error creating new user:", err)
|
||||
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||
return
|
||||
}
|
||||
err = b.userTable.Insert(u)
|
||||
if err != nil {
|
||||
log.Println("error inserting new user:", err)
|
||||
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||
return
|
||||
}
|
||||
@@ -169,6 +173,7 @@ func (b *Backend) createUser(w http.ResponseWriter, r *http.Request) {
|
||||
ret.Username = u.Username
|
||||
ret.Token, err = b.GenerateJWT(u.toReqUser())
|
||||
if err != nil {
|
||||
log.Println("error generating token:", err)
|
||||
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||
return
|
||||
}
|
||||
@@ -243,12 +248,14 @@ func (b *Backend) login(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
hash, err := u.HashPassword(req.Password)
|
||||
if err != nil {
|
||||
log.Println("error hashing request password:", err)
|
||||
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||
return
|
||||
}
|
||||
if u.Password == hash {
|
||||
ret.Token, err = b.GenerateJWT(u.toReqUser())
|
||||
if err != nil {
|
||||
log.Println("error generating token:", err)
|
||||
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -119,6 +119,7 @@ func (b *BlogApp) LatestBlogs(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
blogs, err := b.GetLatestBlogs(int64(page))
|
||||
if err != nil && err != backend.ErrNotFound {
|
||||
log.Println("error getting latest blogs:", err)
|
||||
backend.ReturnError(w, http.StatusInternalServerError, "internal", "internal error")
|
||||
return
|
||||
}
|
||||
@@ -168,6 +169,7 @@ func (b *BlogApp) BlogList(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
blogList, err := b.GetBlogList(int64(page))
|
||||
if err != nil && err != backend.ErrNotFound {
|
||||
log.Println("error getting blog list:", err)
|
||||
backend.ReturnError(w, http.StatusInternalServerError, "internal", "internal error")
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user