error handling for tracked players having no match found

This commit is contained in:
2022-11-16 08:33:09 +01:00
parent cd58741188
commit 40f95cfb6e
2 changed files with 38 additions and 33 deletions

40
main.go
View File

@@ -6,6 +6,7 @@ import (
"encoding/gob" "encoding/gob"
"entgo.io/ent/dialect" "entgo.io/ent/dialect"
"entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql"
"errors"
"flag" "flag"
"fmt" "fmt"
"git.harting.dev/csgowtf/csgowtfd/csgo" "git.harting.dev/csgowtf/csgowtfd/csgo"
@@ -215,18 +216,24 @@ func housekeeping() {
for _, tPlayer := range tPlayerNeedShareCodeUpdate { for _, tPlayer := range tPlayerNeedShareCodeUpdate {
shareCodes, err := utils.GetNewShareCodesForPlayer(tPlayer, conf.Steam.APIKey, rL) shareCodes, err := utils.GetNewShareCodesForPlayer(tPlayer, conf.Steam.APIKey, rL)
if err != nil { if err != nil {
switch err.(type) { if errors.Is(err, utils.AuthcodeUnauthorizedError) {
case utils.AuthcodeUnauthorizedError:
log.Infof("[HK] authCode for player %d is no longer valid", tPlayer.ID) log.Infof("[HK] authCode for player %d is no longer valid", tPlayer.ID)
err = tPlayer.Update().ClearAuthCode().ClearSharecodeUpdated().Exec(context.Background()) err = tPlayer.Update().ClearAuthCode().ClearSharecodeUpdated().Exec(context.Background())
if err != nil { if err != nil {
log.Errorf("[HK] Unable to clear authcode for player %d: %v", tPlayer.ID, err) log.Errorf("[HK] Unable to clear authcode for player %d: %v", tPlayer.ID, err)
} }
continue continue
case utils.SharecodeNoMatchError: } else if errors.Is(err, utils.SharecodeNoMatchError) {
log.Warningf("[HK] last shareCode for player %d does not match player", tPlayer.ID) log.Warningf("[HK] last shareCode for player %d does not match player", tPlayer.ID)
continue continue
default: } else if errors.Is(err, utils.NoMatchError) {
log.Infof("[HK] tracked player with no matched found, untrack him")
err = tPlayer.Update().ClearAuthCode().ClearSharecodeUpdated().Exec(context.Background())
if err != nil {
log.Errorf("[HK] Unable to clear authcode for player %d: %v", tPlayer.ID, err)
}
continue
} else {
log.Errorf("[HK] Error while requesting sharecodes for %d: %v", tPlayer.ID, err) log.Errorf("[HK] Error while requesting sharecodes for %d: %v", tPlayer.ID, err)
continue continue
} }
@@ -477,16 +484,14 @@ func deletePlayerTrack(c *gin.Context) {
_, err = utils.IsAuthCodeValid(tPlayer, conf.Steam.APIKey, "", authCode, nil) _, err = utils.IsAuthCodeValid(tPlayer, conf.Steam.APIKey, "", authCode, nil)
if err != nil { if err != nil {
switch e := err.(type) { if errors.Is(err, utils.AuthcodeUnauthorizedError) {
case utils.AuthcodeUnauthorizedError: log.Infof("[DPT] authCode provided for player %s is invalid: %v", id, err)
log.Infof("[DPT] authCode provided for player %s is invalid: %v", id, e)
c.Status(http.StatusUnauthorized) c.Status(http.StatusUnauthorized)
return return
default:
log.Infof("[DPT] Temporary Steam-API problem: %v", e)
c.Status(http.StatusServiceUnavailable)
return
} }
log.Infof("[DPT] Temporary Steam-API problem: %v", err)
c.Status(http.StatusServiceUnavailable)
return
} }
err = tPlayer.Update().ClearAuthCode().ClearSharecodeUpdated().Exec(context.Background()) err = tPlayer.Update().ClearAuthCode().ClearSharecodeUpdated().Exec(context.Background())
@@ -528,17 +533,16 @@ func postPlayerTrack(c *gin.Context) {
_, err = utils.IsAuthCodeValid(tPlayer, conf.Steam.APIKey, shareCode, authCode, rL) _, err = utils.IsAuthCodeValid(tPlayer, conf.Steam.APIKey, shareCode, authCode, rL)
if err != nil { if err != nil {
switch e := err.(type) { if errors.Is(err, utils.AuthcodeUnauthorizedError) {
case utils.AuthcodeUnauthorizedError: log.Infof("[PPT] authCode provided for player %s is invalid: %v", id, err)
log.Infof("[PPT] authCode provided for player %s is invalid: %v", id, e)
c.Status(http.StatusUnauthorized) c.Status(http.StatusUnauthorized)
return return
case utils.SharecodeNoMatchError: } else if errors.Is(err, utils.SharecodeNoMatchError) {
log.Infof("[PPT] shareCode provided for player %s (%s) is invalid: %v", id, shareCode, e) log.Infof("[PPT] shareCode provided for player %s (%s) is invalid: %v", id, shareCode, err)
c.Status(http.StatusPreconditionFailed) c.Status(http.StatusPreconditionFailed)
return return
default: } else {
log.Infof("[PPT] problem with request: %v", e) log.Infof("[PPT] problem with request: %v", err)
c.Status(http.StatusServiceUnavailable) c.Status(http.StatusServiceUnavailable)
return return
} }

View File

@@ -217,16 +217,11 @@ type MatchResponse struct {
TickRate float64 `json:"tick_rate,omitempty"` TickRate float64 `json:"tick_rate,omitempty"`
} }
type ( var (
AuthcodeUnauthorizedError struct { SharecodeNoMatchError = errors.New("sharecode not provided")
error AuthcodeRateLimitError = errors.New("temporary rate-limited/unavailable")
} AuthcodeUnauthorizedError = errors.New("authcode unauthorized")
AuthcodeRateLimitError struct { NoMatchError = errors.New("no match found")
error
}
SharecodeNoMatchError struct {
error
}
) )
const ( const (
@@ -485,7 +480,7 @@ func IsAuthCodeValid(player *ent.Player, apiKey string, shareCode string, authCo
if shareCode == "" { if shareCode == "" {
tMatch, err = player.QueryMatches().Order(ent.Asc(match.FieldDate)).First(context.Background()) tMatch, err = player.QueryMatches().Order(ent.Asc(match.FieldDate)).First(context.Background())
if err != nil { if err != nil {
return false, SharecodeNoMatchError{errors.New("sharecode not provided")} return false, SharecodeNoMatchError
} }
_, err := getNextShareCode(tMatch.ShareCode, apiKey, authCode, player.ID, rl) _, err := getNextShareCode(tMatch.ShareCode, apiKey, authCode, player.ID, rl)
@@ -505,11 +500,17 @@ func IsAuthCodeValid(player *ent.Player, apiKey string, shareCode string, authCo
func GetNewShareCodesForPlayer(player *ent.Player, apiKey string, rl *rate.Limiter) ([]string, error) { func GetNewShareCodesForPlayer(player *ent.Player, apiKey string, rl *rate.Limiter) ([]string, error) {
latestMatch, err := player.QueryMatches().Order(ent.Desc(match.FieldDate)).First(context.Background()) latestMatch, err := player.QueryMatches().Order(ent.Desc(match.FieldDate)).First(context.Background())
if err != nil { if err != nil {
if ent.IsNotFound(err) {
return nil, NoMatchError
}
return nil, err return nil, err
} }
oldestMatch, err := player.QueryMatches().Order(ent.Asc(match.FieldDate)).First(context.Background()) oldestMatch, err := player.QueryMatches().Order(ent.Asc(match.FieldDate)).First(context.Background())
if err != nil { if err != nil {
if ent.IsNotFound(err) {
return nil, NoMatchError
}
return nil, err return nil, err
} }
@@ -561,13 +562,13 @@ func getNextShareCode(lastCode string, apiKey string, authCode string, steamId u
case http.StatusAccepted: case http.StatusAccepted:
return "n/a", nil return "n/a", nil
case http.StatusTooManyRequests: case http.StatusTooManyRequests:
return "", AuthcodeRateLimitError{errors.New("temporary ratelimited")} return "", AuthcodeRateLimitError
case http.StatusServiceUnavailable: case http.StatusServiceUnavailable:
return "", AuthcodeRateLimitError{errors.New("temporary unavailable")} return "", AuthcodeRateLimitError
case http.StatusPreconditionFailed: case http.StatusPreconditionFailed:
return "", SharecodeNoMatchError{errors.New("sharecode not from player history")} return "", SharecodeNoMatchError
case http.StatusForbidden: case http.StatusForbidden:
return "", AuthcodeUnauthorizedError{errors.New("authcode unauthorized")} return "", AuthcodeUnauthorizedError
case http.StatusOK: case http.StatusOK:
break break
default: default: