From a502134c0aecc7fed38d464f2df409a6272e1e12 Mon Sep 17 00:00:00 2001 From: Eli Bosley Date: Mon, 3 Feb 2025 10:47:35 -0500 Subject: [PATCH] fix: better loader functionality and error handling --- .../unraid-file-modifier/file-modification.ts | 10 ++++++---- .../unraid-file-modifier/unraid-file-modifier.spec.ts | 6 +++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/api/src/unraid-api/unraid-file-modifier/file-modification.ts b/api/src/unraid-api/unraid-file-modifier/file-modification.ts index 6e8a3b701..3c2953232 100644 --- a/api/src/unraid-api/unraid-file-modifier/file-modification.ts +++ b/api/src/unraid-api/unraid-file-modifier/file-modification.ts @@ -46,9 +46,10 @@ export abstract class FileModification { query: '?raw', import: 'default', }); + const patchPath = `./modifications/patches/${this.id}.patch`; + const loader = patchResults[patchPath]; - if (patchResults[`./modifications/patches/${this.id}.patch`]) { - const loader = Object.values(patchResults)[0]; + if (typeof loader === 'function') { const fileContents = await loader(); this.logger.debug(`Loaded pregenerated patch for ${this.id}`); if (typeof fileContents !== 'string') { @@ -56,9 +57,10 @@ export abstract class FileModification { return null; } return fileContents; + } else { + this.logger.warn('Could not load pregenerated patch for: ' + this.id); + return null; } - - return null; } private async applyPatch(patchContents: string): Promise { diff --git a/api/src/unraid-api/unraid-file-modifier/unraid-file-modifier.spec.ts b/api/src/unraid-api/unraid-file-modifier/unraid-file-modifier.spec.ts index 95dd32cf0..26503ef0a 100644 --- a/api/src/unraid-api/unraid-file-modifier/unraid-file-modifier.spec.ts +++ b/api/src/unraid-api/unraid-file-modifier/unraid-file-modifier.spec.ts @@ -99,7 +99,7 @@ describe.sequential('FileModificationService', () => { const rolledBackContent = await fs.readFile(FIXTURE_PATH, 'utf-8'); expect(rolledBackContent).toBe(ORIGINAL_CONTENT); - expect(mockLogger.error).not.toHaveBeenCalled(); + expect(mockLogger.warn).toHaveBeenCalledWith('Could not load pregenerated patch for: test'); expect(mockLogger.log.mock.calls).toEqual([ ['RootTestModule dependencies initialized'], ['Applying modification: test - Always Apply this mod'], @@ -126,9 +126,9 @@ describe.sequential('FileModificationService', () => { // Now apply again and ensure the contents don't change await service.applyModification(mod); - const errorMessage = mockLogger.error.mock.calls[0][0]; + const errorMessage = mockLogger.warn.mock.calls[0][0]; expect(errorMessage).toContain( - 'Failed to apply patch to /app/src/unraid-api/unraid-file-modifier/modifications/__fixtures__/text-patch-file.txt' + 'Could not load pregenerated patch for: test' ); });