Skip to content

Commit

Permalink
Merge pull request #1471 from rommapp/hotfix-first-release-date
Browse files Browse the repository at this point in the history
[HOTFIX] Safe cast values to int for first_release_date
  • Loading branch information
gantoine authored Jan 12, 2025
2 parents 1f1bd35 + aacad9c commit c731263
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 5 additions & 4 deletions backend/models/rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
func,
)
from sqlalchemy.orm import Mapped, mapped_column, relationship
from utils.database import CustomJSON, safe_float
from utils.database import CustomJSON, safe_float, safe_int

if TYPE_CHECKING:
from models.assets import Save, Screenshot, State
Expand Down Expand Up @@ -165,10 +165,11 @@ def alternative_names(self) -> list[str]:
)

@property
def first_release_date(self) -> int:
def first_release_date(self) -> int | None:
if self.igdb_metadata:
return self.igdb_metadata.get("first_release_date", 0) * 1000
return 0
return safe_int(self.igdb_metadata.get("first_release_date") or 0) * 1000

return None

@property
def average_rating(self) -> float | None:
Expand Down
8 changes: 8 additions & 0 deletions backend/utils/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,11 @@ def safe_float(value, default=0.0):
return float(value)
except (ValueError, TypeError):
return default


def safe_int(value, default=0):
"""Safely convert a value to int, returning default if conversion fails."""
try:
return int(value)
except (ValueError, TypeError):
return default

0 comments on commit c731263

Please sign in to comment.