better repeat error parsing
This commit is contained in:
1 parent
f8f6d74d6a
commit
9184d1f7bd
8 files changed
+65
-31
No files matched your search
@@ -1,10 +1,10 @@
|
||||
from ctypes import Union
|
||||
from fastapi import APIRouter, HTTPException
|
||||
|
||||
from src.api.types import ListData, ListParams, UpdateTags
|
||||
from src.automations.errors import FailedDeletion
|
||||
from src.automations.manager import AutomationManager
|
||||
from src.automations.types import Automation, ExtenededAutomation
|
||||
from src.errors import ErrorSet
|
||||
|
||||
|
||||
def make_automation_router(automations: AutomationManager) -> APIRouter:
|
||||
@@ -12,15 +12,18 @@ def make_automation_router(automations: AutomationManager) -> APIRouter:
|
||||
|
||||
@router.post("/list")
|
||||
def list_autos(body: ListParams) -> ListData[ExtenededAutomation]:
|
||||
automations.reload()
|
||||
return ListData(
|
||||
params=body,
|
||||
totalItems=automations.count(),
|
||||
data=automations.list(
|
||||
offset=body.offset,
|
||||
limit=body.limit,
|
||||
),
|
||||
)
|
||||
try:
|
||||
automations.reload()
|
||||
return ListData(
|
||||
params=body,
|
||||
totalItems=automations.count(),
|
||||
data=automations.list(
|
||||
offset=body.offset,
|
||||
limit=body.limit,
|
||||
),
|
||||
)
|
||||
except ErrorSet as err:
|
||||
raise HTTPException(status_code=400, detail=err.gen_message())
|
||||
|
||||
@router.post("/item")
|
||||
def insert_auto(body: Automation):
|
||||
|
||||
+20
-17
@@ -7,7 +7,7 @@ from typing import Iterable, Iterator, List, Optional
|
||||
|
||||
from src.logger import get_logger
|
||||
|
||||
from .errors import DBDataError, DBError, DBNoAutomationFound
|
||||
from .errors import DBDataError, DBError, DBNoAutomationFound, RepeatedAutomationId
|
||||
from .types import ExtenededAutomation
|
||||
|
||||
logger = get_logger(__file__)
|
||||
@@ -47,8 +47,6 @@ class AutomationDBConnection:
|
||||
try:
|
||||
yield cur
|
||||
conn.commit()
|
||||
except sqlite3.IntegrityError as err:
|
||||
raise DBDataError(*err.args) from err
|
||||
except sqlite3.DatabaseError as err:
|
||||
raise DBError(*err.args) from err
|
||||
finally:
|
||||
@@ -75,20 +73,25 @@ class AutomationDBConnection:
|
||||
def insert_automations(self, automations: List[ExtenededAutomation]):
|
||||
with self.get_cur() as cur:
|
||||
for automation in automations:
|
||||
cur.execute(
|
||||
f"""
|
||||
INSERT INTO {automations_tbl} VALUES (
|
||||
"{automation.id}",
|
||||
"{automation.alias}",
|
||||
"{automation.description}",
|
||||
"{automation.mode}",
|
||||
"{automation.source_file}",
|
||||
"{automation.source_file_type}",
|
||||
"{automation.configuration_key}",
|
||||
"{encode_auto(automation)}"
|
||||
);
|
||||
""",
|
||||
)
|
||||
try:
|
||||
cur.execute(
|
||||
f"""
|
||||
INSERT INTO {automations_tbl} VALUES (
|
||||
"{automation.id}",
|
||||
"{automation.alias}",
|
||||
"{automation.description}",
|
||||
"{automation.mode}",
|
||||
"{automation.source_file}",
|
||||
"{automation.source_file_type}",
|
||||
"{automation.configuration_key}",
|
||||
"{encode_auto(automation)}"
|
||||
);
|
||||
""",
|
||||
)
|
||||
except sqlite3.IntegrityError as err:
|
||||
raise RepeatedAutomationId(
|
||||
f"automation id '{automation.id}' in '{automation.source_file}' was already seen before. Please check for duplicate automation ids in your files."
|
||||
) from err
|
||||
|
||||
def get_automation(self, automation_id: str) -> ExtenededAutomation:
|
||||
with self.get_cur() as cur:
|
||||
|
||||
@@ -25,5 +25,9 @@ class DBDataError(DBError):
|
||||
pass
|
||||
|
||||
|
||||
class RepeatedAutomationId(DBDataError):
|
||||
pass
|
||||
|
||||
|
||||
class DBNoAutomationFound(DBError):
|
||||
pass
|
||||
@@ -60,7 +60,7 @@ class db_tests(TestWithDB):
|
||||
configuration_key="automation",
|
||||
),
|
||||
]
|
||||
with self.assertRaises(DBDataError):
|
||||
with self.assertRaises(DBDataError) as err:
|
||||
self.db.insert_automations(originals)
|
||||
autos = list(self.db.list_automations(0, 10))
|
||||
self.assertEqual(len(autos), 0)
|
||||
Reference in new issue
Block a user