Entire logging rework. Full error handling. Small improvements.

This commit is contained in:
MaxJa4
2024-01-22 17:24:17 +01:00
parent da1ff4e4e5
commit ca697da0da
18 changed files with 278 additions and 205 deletions

View File

@@ -24,12 +24,14 @@ func GetCacheByPlayerID(c *gin.Context) {
game, err := GetActiveGame(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "No active game available!"})
utils.Logger.Warnf("[CACHE] No active game available! Error: %s", err.Error())
return
}
val, err := models.PlayerCache.GetScore(playerId, game.Tag)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Record not found!"})
utils.Logger.Warnf("[CACHE] Record not found! Error: %s", err.Error())
} else {
c.JSON(http.StatusOK, val)
}
@@ -38,20 +40,24 @@ func GetCacheByPlayerID(c *gin.Context) {
// AddCache POST /cache
func AddCache(c *gin.Context) {
var input AddCacheInput
var game models.Game
if err := c.BindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
utils.Logger.Warnf("[CACHE] Failed to bind JSON! Error: %s", err.Error())
return
}
var game models.Game
if err := FindGameByTag(&game, input.GameTag).Error; err != nil {
if err := models.DB.Where("tag = ?", input.GameTag).First(&game).Error; err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Game not found!"})
utils.Logger.Warnf("[CACHE] Game not found! Error: %s", err.Error())
return
}
err := models.PlayerCache.SetScore(input.PlayerID, input.GameTag, input.Score)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Cache update failed! Error: " + err.Error()})
utils.Logger.Warnf("[CACHE] Cache update failed! Error: %s", err.Error())
return
}
@@ -67,6 +73,7 @@ func UpdateCacheByPlayerID(c *gin.Context) {
err := models.PlayerCache.SetScore(playerID, gameTag, score)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Cache update failed! Error: " + err.Error()})
utils.Logger.Warnf("[CACHE] Cache update failed! Error: %s", err.Error())
} else {
c.JSON(http.StatusOK, nil)
}
@@ -79,6 +86,7 @@ func DeleteCacheByPlayerID(c *gin.Context) {
if err := models.PlayerCache.DeleteScore(playerID, gameTag); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
utils.Logger.Warnf("[CACHE] Cache deletion failed! Error: %s", err.Error())
} else {
c.JSON(http.StatusOK, nil)
}
@@ -88,13 +96,14 @@ func DeleteCacheByPlayerID(c *gin.Context) {
func DeleteAllCaches(c *gin.Context) {
if err := models.PlayerCache.PurgeCache(); err != nil {
c.String(http.StatusBadRequest, err.Error())
utils.Logger.Warnf("[CACHE] Cache purge failed! Error: %s", err.Error())
} else {
c.String(http.StatusOK, "Purged all caches!")
}
}
func UpdateCacheAfterExpiry(key string) {
utils.Logger.Println("[KeepUpdated] Cache expired: " + key)
utils.Logger.Infof("[KeepUpdated] Updating cache for key %s", key)
playerId, err := models.PlayerCache.GetPlayerIdFromCacheKey(key)
if err != nil {
@@ -107,19 +116,20 @@ func UpdateCacheAfterExpiry(key string) {
Where("id = ?", playerId).
First(&player).Error; err != nil {
utils.Logger.Println("Failed to find player: " + err.Error())
utils.Logger.Errorf("[KeepUpdated] Failed to find player with ID %d! Error: %s", playerId, err.Error())
return
}
if player.Clan.KeepUpdated {
score := CalcPlayerScore(player.Name, "BF2042")
if score == score && score != -1 { // not NaN
if err := models.PlayerCache.SetScore(player.ID, "BF2042", score); err != nil {
utils.Logger.Println("Failed to update cache: " + err.Error())
utils.Logger.Warnf("[KeepUpdated] Failed to update cache for player %s! Error: %s", player.Name, err.Error())
} else {
utils.Logger.Println("[KeepUpdated] Updated cache for player " + player.Name)
utils.Logger.Infof("[KeepUpdated] Updated cache for player %s", player.Name)
}
} else {
utils.Logger.Println("[KeepUpdated] Received NaN for player " + player.Name)
utils.Logger.Warnf("[KeepUpdated] Failed to calculate score for player %s", player.Name)
}
}
}