Skip to content

Commit

Permalink
Revised error handling for duplicate datetimes.
Browse files Browse the repository at this point in the history
Previous implementation of #510 meant that any database related exception would result in 500. As suggested in #538, duplicate data being sent to the server does not necessarily constitute an error from the external perspective. This commit introduce code that pass on exceptions for primary key violations (duplicate resultid and datetime) and will still return a 201.
  • Loading branch information
ptomasula committed Dec 17, 2021
1 parent 5ca7588 commit e43ff3a
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/dataloaderservices/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#PRT - temporary work around after replacing InfluxDB but not replacement models
import sqlalchemy
from sqlalchemy.sql import text
import psycopg2
from django.conf import settings
_dbsettings = settings.DATABASES['odm2']
_connection_str = f"postgresql://{_dbsettings['USER']}:{_dbsettings['PASSWORD']}@{_dbsettings['HOST']}:{_dbsettings['PORT']}/{_dbsettings['NAME']}"
Expand Down Expand Up @@ -609,6 +610,14 @@ def post(self, request, format=None):

try:
query_result = InsertTimeseriesResultValues(result_value)
except sqlalchemy.exc.IntegrityError as e:
if hasattr(e, 'orig'):
if isinstance(e.orig, psycopg2.errors.UniqueViolation):
pass #data is already in database
else:
errors.append(f"Failed to INSERT data for uuid('{result.result_uuid}')")
else:
errors.append(f"Failed to INSERT data for uuid('{result.result_uuid}')")
except Exception as e:
errors.append(f"Failed to INSERT data for uuid('{result.result_uuid}')")

Expand Down

0 comments on commit e43ff3a

Please sign in to comment.