fix: better loader functionality and error handling

This commit is contained in:
Eli Bosley
2025-02-03 10:47:35 -05:00
parent fa16dcd801
commit a502134c0a
2 changed files with 9 additions and 7 deletions

View File

@@ -46,9 +46,10 @@ export abstract class FileModification {
query: '?raw', query: '?raw',
import: 'default', import: 'default',
}); });
const patchPath = `./modifications/patches/${this.id}.patch`;
const loader = patchResults[patchPath];
if (patchResults[`./modifications/patches/${this.id}.patch`]) { if (typeof loader === 'function') {
const loader = Object.values(patchResults)[0];
const fileContents = await loader(); const fileContents = await loader();
this.logger.debug(`Loaded pregenerated patch for ${this.id}`); this.logger.debug(`Loaded pregenerated patch for ${this.id}`);
if (typeof fileContents !== 'string') { if (typeof fileContents !== 'string') {
@@ -56,9 +57,10 @@ export abstract class FileModification {
return null; return null;
} }
return fileContents; return fileContents;
} else {
this.logger.warn('Could not load pregenerated patch for: ' + this.id);
return null;
} }
return null;
} }
private async applyPatch(patchContents: string): Promise<void> { private async applyPatch(patchContents: string): Promise<void> {

View File

@@ -99,7 +99,7 @@ describe.sequential('FileModificationService', () => {
const rolledBackContent = await fs.readFile(FIXTURE_PATH, 'utf-8'); const rolledBackContent = await fs.readFile(FIXTURE_PATH, 'utf-8');
expect(rolledBackContent).toBe(ORIGINAL_CONTENT); 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([ expect(mockLogger.log.mock.calls).toEqual([
['RootTestModule dependencies initialized'], ['RootTestModule dependencies initialized'],
['Applying modification: test - Always Apply this mod'], ['Applying modification: test - Always Apply this mod'],
@@ -126,9 +126,9 @@ describe.sequential('FileModificationService', () => {
// Now apply again and ensure the contents don't change // Now apply again and ensure the contents don't change
await service.applyModification(mod); await service.applyModification(mod);
const errorMessage = mockLogger.error.mock.calls[0][0]; const errorMessage = mockLogger.warn.mock.calls[0][0];
expect(errorMessage).toContain( 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'
); );
}); });