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.');
}