From f26ca5aa62ac61bb8d57ff386f254b59a7826efb Mon Sep 17 00:00:00 2001 From: phact Date: Wed, 28 Aug 2024 14:31:44 -0400 Subject: [PATCH] don't crash on invalid objects from the db --- impl/utils.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/impl/utils.py b/impl/utils.py index dab48d6..de563c4 100644 --- a/impl/utils.py +++ b/impl/utils.py @@ -7,6 +7,7 @@ import traceback from typing import Type, Dict, Any, List, get_origin, Annotated, get_args, Union +import pydantic from fastapi import HTTPException from pydantic import BaseModel @@ -151,9 +152,14 @@ def read_objects(astradb: CassandraClient, target_class: Type[BaseModel], table_ json_obj[field_name] = int(json_obj[field_name].timestamp()*1000) elif annotation is int and isinstance(json_obj[field_name], datetime.datetime): json_obj[field_name] = int(json_obj[field_name].timestamp()*1000) - - obj = target_class(**json_obj) - obj_list.append(obj) + try: + obj = target_class(**json_obj) + obj_list.append(obj) + except Exception as e: + if isinstance(e, pydantic.ValidationError): + logger.warn(f"ignoring bad object from {table_name} - {e} json_obj: {json_obj}") + else: + raise HTTPException(status_code=500, detail=f"Error reading {table_name}: {e}") return obj_list except Exception as e: if hasattr(e, 'status_code') and e.status_code== 404: