Skip to content

Commit

Permalink
feat: properly handle failed achievement fetches
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Gleich <[email protected]>
  • Loading branch information
gleich committed Dec 11, 2024
1 parent 894a7df commit 3a0c414
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
22 changes: 11 additions & 11 deletions internal/apis/steam/achievements.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type achievement struct {
UnlockTime *time.Time `json:"unlock_time"`
}

func fetchGameAchievements(appID int32) (*float32, *[]achievement) {
func fetchGameAchievements(appID int32) (*float32, *[]achievement, error) {
params := url.Values{
"key": {secrets.SECRETS.SteamKey},
"steamid": {secrets.SECRETS.SteamID},
Expand All @@ -59,17 +59,17 @@ func fetchGameAchievements(appID int32) (*float32, *[]achievement) {
)
if err != nil {
lumber.Error(err, "sending request for player achievements from", appID, "failed")
return nil, nil
return nil, nil, err
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
lumber.Error(err, "reading response body for player achievements from", appID, "failed")
return nil, nil
return nil, nil, err
}
if string(body) == `{"playerstats":{"error":"Requested app has no stats","success":false}}` {
return nil, nil
return nil, nil, err
}
if resp.StatusCode != http.StatusOK {
err := fmt.Errorf(
Expand All @@ -81,23 +81,23 @@ func fetchGameAchievements(appID int32) (*float32, *[]achievement) {
resp.StatusCode == http.StatusGatewayTimeout ||
resp.StatusCode == http.StatusInternalServerError {
lumber.Warning(err)
return nil, nil
return nil, nil, apis.WarningError
} else {
lumber.Error(err)
}
return nil, nil
return nil, nil, err
}

var playerAchievements playerAchievementsResponse
err = json.Unmarshal(body, &playerAchievements)
if err != nil {
lumber.Error(err, "failed to parse json for player achievements for", appID)
lumber.Debug("body:", string(body))
return nil, nil
return nil, nil, err
}

if playerAchievements.PlayerStats.Achievements == nil {
return nil, nil
return nil, nil, err
}

params = url.Values{
Expand All @@ -112,14 +112,14 @@ func fetchGameAchievements(appID int32) (*float32, *[]achievement) {
)
if err != nil {
lumber.Error(err, "creating request for owned games failed for app id:", appID)
return nil, nil
return nil, nil, err
}
gameSchema, err := apis.SendRequest[schemaGameResponse](req)
if err != nil {
if !errors.Is(err, apis.WarningError) {
lumber.Error(err, "failed to get game schema for app id:", appID)
}
return nil, nil
return nil, nil, err
}

var achievements []achievement
Expand Down Expand Up @@ -167,5 +167,5 @@ func fetchGameAchievements(appID int32) (*float32, *[]achievement) {
achievements = achievements[:5]
}

return &achievementPercentage, &achievements
return &achievementPercentage, &achievements, nil
}
5 changes: 4 additions & 1 deletion internal/apis/steam/games.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ func fetchRecentlyPlayedGames() ([]game, error) {
libraryURLPtr = &libraryURL
}

achievementPercentage, achievements := fetchGameAchievements(g.AppID)
achievementPercentage, achievements, err := fetchGameAchievements(g.AppID)
if err != nil {
return nil, err
}

games = append(games, game{
Name: g.Name,
Expand Down

0 comments on commit 3a0c414

Please sign in to comment.