refactor: add guards around trivial index checks

This commit is contained in:
Carlos Valente
2025-12-11 17:59:46 +01:00
committed by Carlos Valente
parent 242cefd35c
commit 426d42ab63
7 changed files with 35 additions and 8 deletions
@@ -72,9 +72,13 @@ export async function editTrigger(id: string, newTrigger: TriggerDTO): Promise<T
throw new Error(`Automation with id ${id} not found`); throw new Error(`Automation with id ${id} not found`);
} }
triggers[index] = { ...triggers[index], ...newTrigger }; triggers[index] = { ...triggers[index], ...newTrigger, id };
await saveChanges({ triggers }); await saveChanges({ triggers });
return triggers[index]; const updatedTrigger = triggers[index];
if (!updatedTrigger) {
throw new Error(`Failed to update trigger with id ${id}`);
}
return updatedTrigger;
} }
/** /**
@@ -145,8 +149,10 @@ export async function deleteAutomation(projectRundowns: ProjectRundowns, automat
// prevent deleting a automation that is in use in triggers // prevent deleting a automation that is in use in triggers
const triggers = getAutomationTriggers().filter((trigger) => trigger.automationId === automationId); const triggers = getAutomationTriggers().filter((trigger) => trigger.automationId === automationId);
if (triggers.length) { if (triggers.length) {
const firstTrigger = triggers[0];
const triggerTitle = firstTrigger?.title ?? 'Unknown trigger';
throw new Error( throw new Error(
`Unable to delete automation used in trigger ${triggers[0].title}${triggers.length > 1 ? ` and ${triggers.length - 1} more` : ''}`, `Unable to delete automation used in trigger ${triggerTitle}${triggers.length > 1 ? ` and ${triggers.length - 1} more` : ''}`,
); );
} }
@@ -213,6 +213,9 @@ function add(rundown: Rundown, entry: OntimeEntry, afterId: EntryId | null, pare
*/ */
function edit(rundown: Rundown, patch: PatchWithId): { entry: OntimeEntry; didInvalidate: boolean } { function edit(rundown: Rundown, patch: PatchWithId): { entry: OntimeEntry; didInvalidate: boolean } {
const entry = rundown.entries[patch.id]; const entry = rundown.entries[patch.id];
if (!entry) {
throw new Error(`Entry with id ${patch.id} not found`);
}
// apply the patch and replace the entry // apply the patch and replace the entry
const newEntry = applyPatchToEntry(entry, patch); const newEntry = applyPatchToEntry(entry, patch);
@@ -31,8 +31,9 @@ export function catchCommonImportXlsxError(error: any) {
isGoogleApiError(error) && isGoogleApiError(error) &&
error.code === 400 && error.code === 400 &&
Array.isArray(error.errors) && Array.isArray(error.errors) &&
error.errors[0].reason === 'failedPrecondition' && error.errors.length > 0 &&
error.errors[0].message === 'This operation is not supported for this document' error.errors[0]?.reason === 'failedPrecondition' &&
error.errors[0]?.message === 'This operation is not supported for this document'
) { ) {
throw new Error('Cannot read the linked file as a Google Sheet. It may be an .xlsx file instead.'); throw new Error('Cannot read the linked file as a Google Sheet. It may be an .xlsx file instead.');
} }
@@ -150,7 +150,9 @@ async function setAutomation(newData: AutomationSettings): ReadonlyPromise<Autom
function getRundown(rundownKey: string): Readonly<Rundown> { function getRundown(rundownKey: string): Readonly<Rundown> {
if (!(rundownKey in db.data.rundowns)) throw new Error(`Rundown with id: ${rundownKey} not found`); if (!(rundownKey in db.data.rundowns)) throw new Error(`Rundown with id: ${rundownKey} not found`);
return db.data.rundowns[rundownKey]; const rundown = db.data.rundowns[rundownKey];
if (!rundown) throw new Error(`Rundown with id: ${rundownKey} not found`);
return rundown;
} }
async function deleteRundown(rundownKey: string): Promise<ProjectRundowns> { async function deleteRundown(rundownKey: string): Promise<ProjectRundowns> {
@@ -94,6 +94,10 @@ async function loadProject(projectData: DatabaseModel, fileName: string, rundown
? projectData.rundowns[rundownId] ? projectData.rundowns[rundownId]
: getFirstRundown(projectData.rundowns); : getFirstRundown(projectData.rundowns);
if (!rundown) {
throw new Error('No rundown found in project');
}
await initRundown(rundown, projectData.customFields, true); await initRundown(rundown, projectData.customFields, true);
// persist the project selection // persist the project selection
+3 -1
View File
@@ -28,7 +28,9 @@ export function loadRoll(
let daySpan = 0; let daySpan = 0;
for (let i = 0; i < metadata.playableEventOrder.length; i++) { for (let i = 0; i < metadata.playableEventOrder.length; i++) {
const event = rundown.entries[metadata.playableEventOrder[i]] as PlayableEvent; const eventId = metadata.playableEventOrder[i];
if (!eventId) continue;
const event = rundown.entries[eventId] as PlayableEvent;
// rolling into events of 0 duration would make the playback be stuck // rolling into events of 0 duration would make the playback be stuck
if (event.duration === 0) { if (event.duration === 0) {
continue; continue;
+10 -1
View File
@@ -276,7 +276,12 @@ export function loadNow(
return; return;
} }
const event = rundown.entries[metadata.timedEventOrder[eventIndex]] as PlayableEvent; const eventId = metadata.timedEventOrder[eventIndex];
if (!eventId) {
runtimeState.eventNow = null;
return;
}
const event = rundown.entries[eventId] as PlayableEvent;
runtimeState.rundown.selectedEventIndex = eventIndex; runtimeState.rundown.selectedEventIndex = eventIndex;
runtimeState.eventNow = event; runtimeState.eventNow = event;
} }
@@ -302,6 +307,10 @@ export function loadNext(
return; return;
} }
const nextId = metadata.playableEventOrder[nowPlayableIndex + 1]; const nextId = metadata.playableEventOrder[nowPlayableIndex + 1];
if (!nextId) {
runtimeState.eventNext = null;
return;
}
runtimeState.eventNext = rundown.entries[nextId] as PlayableEvent; runtimeState.eventNext = rundown.entries[nextId] as PlayableEvent;
} }