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`);
}
triggers[index] = { ...triggers[index], ...newTrigger };
triggers[index] = { ...triggers[index], ...newTrigger, id };
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
const triggers = getAutomationTriggers().filter((trigger) => trigger.automationId === automationId);
if (triggers.length) {
const firstTrigger = triggers[0];
const triggerTitle = firstTrigger?.title ?? 'Unknown trigger';
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 } {
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
const newEntry = applyPatchToEntry(entry, patch);
@@ -31,8 +31,9 @@ export function catchCommonImportXlsxError(error: any) {
isGoogleApiError(error) &&
error.code === 400 &&
Array.isArray(error.errors) &&
error.errors[0].reason === 'failedPrecondition' &&
error.errors[0].message === 'This operation is not supported for this document'
error.errors.length > 0 &&
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.');
}
@@ -150,7 +150,9 @@ async function setAutomation(newData: AutomationSettings): ReadonlyPromise<Autom
function getRundown(rundownKey: string): Readonly<Rundown> {
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> {
@@ -94,6 +94,10 @@ async function loadProject(projectData: DatabaseModel, fileName: string, rundown
? projectData.rundowns[rundownId]
: getFirstRundown(projectData.rundowns);
if (!rundown) {
throw new Error('No rundown found in project');
}
await initRundown(rundown, projectData.customFields, true);
// persist the project selection
+3 -1
View File
@@ -28,7 +28,9 @@ export function loadRoll(
let daySpan = 0;
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
if (event.duration === 0) {
continue;
+10 -1
View File
@@ -276,7 +276,12 @@ export function loadNow(
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.eventNow = event;
}
@@ -302,6 +307,10 @@ export function loadNext(
return;
}
const nextId = metadata.playableEventOrder[nowPlayableIndex + 1];
if (!nextId) {
runtimeState.eventNext = null;
return;
}
runtimeState.eventNext = rundown.entries[nextId] as PlayableEvent;
}