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)
|
||||
@@ -25,6 +25,11 @@
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--title {
|
||||
flex: 1;
|
||||
max-width: calc(80% - 5em);
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--switch {
|
||||
width: 5em;
|
||||
}
|
||||
|
||||
.automation-manager--metadatabox .metadatabox--title .title {
|
||||
@@ -94,4 +99,5 @@
|
||||
.automation-manager--metadatabox .metadatabox--title--source .MuiChip-root {
|
||||
font-size: 0.85em;
|
||||
height: 2em;
|
||||
max-width: 60%;
|
||||
}
|
||||
@@ -14,7 +14,7 @@ import { ButtonIcon } from "components/Icons/ButtonIcons";
|
||||
import { AutomationManagerAuto } from "../types";
|
||||
import { Tags } from "./Tags";
|
||||
import { TagDB } from "../TagDB";
|
||||
import { Chip } from "@mui/material";
|
||||
import { Chip, Tooltip } from "@mui/material";
|
||||
|
||||
export type MetadataBoxProps = AutomationManagerAuto & {
|
||||
onDelete: () => void;
|
||||
@@ -55,7 +55,14 @@ export const MetadataBox: FC<MetadataBoxProps> = (props) => {
|
||||
title={`ID=${props.id}, EntityID=${props.entityId}`}
|
||||
>
|
||||
<span className="metadatabox--title--source">
|
||||
<Chip label={`@${props.source_file} (${props.source_file_type})`} />
|
||||
<Tooltip
|
||||
title={`This automation is located in the file ${props.source_file} and is set up in your configuration.yaml file under the key ${props.configuration_key}.`}
|
||||
>
|
||||
<Chip
|
||||
label={`@${props.source_file} (${props.source_file_type})`}
|
||||
className="text-ellipsis"
|
||||
/>
|
||||
</Tooltip>
|
||||
</span>
|
||||
<InputTextView
|
||||
className="title"
|
||||
|
||||
@@ -10,6 +10,10 @@
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.automation-manager.error>code {
|
||||
padding: 1.5em;
|
||||
}
|
||||
|
||||
.automation-manager.loading .automation-manager--sidebar.loading {
|
||||
width: 25vh;
|
||||
height: 100%;
|
||||
|
||||
@@ -195,13 +195,20 @@ export const AutomationManager: FC<AutomationManagerProps> = ({
|
||||
}
|
||||
|
||||
if (!api.state.automations.ok) {
|
||||
let error = api.state.automations.error;
|
||||
try {
|
||||
const errorObj = JSON.parse(api.state.automations.error);
|
||||
if ("detail" in errorObj) {
|
||||
error = errorObj["detail"];
|
||||
}
|
||||
} catch (_: any) {}
|
||||
return (
|
||||
<div className="automation-manager error">
|
||||
<Alert color="error">
|
||||
Failed to load automations from <code>/config</code>
|
||||
</Alert>{" "}
|
||||
<br />
|
||||
<code>{api.state.automations.error}</code>
|
||||
<code>{error}</code>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user