Skip to content

Commit

Permalink
Merge pull request #4 from gdman/feature/improveProcessorErrorHandling
Browse files Browse the repository at this point in the history
Improve processor error handling
  • Loading branch information
gdman committed Aug 8, 2022
2 parents 06b3075 + 822d412 commit feef3ef
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
24 changes: 14 additions & 10 deletions src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,25 @@ export async function processor(baseDirOrOptions: string | ProcessorOptions, pat
file.disableParser();
}

const data = await file.read();

const filename = file.getFile().replace(options.baseDir + path.sep, '');

const processorResponse = await options.processorFunction(data.data, filename);
try {
const data = await file.read();

if (data.type === DataType.String && processorResponse === undefined) {
throw new Error('When working with data as a string - the processor function must return data (or null)');
}
const processorResponse = await options.processorFunction(data.data, filename);

if (processorResponse !== undefined) {
data.data = processorResponse;
}
if (data.type === DataType.String && processorResponse === undefined) {
throw new Error('When working with data as a string - the processor function must return data (or null)');
}

await file.write();
if (processorResponse !== undefined) {
data.data = processorResponse;
}

await file.write();
} catch (fileEx) {
throw new Error('Failed to process: ' + filename + '. Error: ' + (fileEx.message || fileEx));
}

updatedFiles.push(filename);
}
Expand Down
10 changes: 5 additions & 5 deletions test/proccessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ describe('processor', () => {
const mockProcessor = jest.fn((data: any, filename: string) => { data.replace('value', 'not-returned') });

return expect(processor(folderPath, '**/*.txt', mockProcessor, ProcessorDataFormat.String))
.rejects.toEqual('Metadata processor failed: When working with data as a string - the processor function must return data (or null)');
.rejects.toEqual('Metadata processor failed: Failed to process: ' + path.join('folder1', 'foldera', 'text-file.txt') + '. Error: When working with data as a string - the processor function must return data (or null)');
});

it('string processor function returns null which is ok and will legitimately empty a file', async () => {
Expand Down Expand Up @@ -523,7 +523,7 @@ describe('processor', () => {
// tslint:disable-next-line:no-string-throw
throw "THROW";
}, ProcessorDataFormat.String)
).rejects.toEqual('Metadata processor failed: THROW');
).rejects.toEqual('Metadata processor failed: Failed to process: ' + path.join('folder1', 'foldera', 'text-file.txt') + '. Error: THROW');
});

it('processing function throws an error (object) - error is caught and processor rejects', () => {
Expand All @@ -534,7 +534,7 @@ describe('processor', () => {
processor(folderPath, '**/*.txt', (data: any, filename: string) => {
throw new Error("THROW");
}, ProcessorDataFormat.String)
).rejects.toEqual('Metadata processor failed: THROW');
).rejects.toEqual('Metadata processor failed: Failed to process: ' + path.join('folder1', 'foldera', 'text-file.txt') + '. Error: THROW');
});

it('processing async function rejects (string) - error is caught and processor rejects', () => {
Expand All @@ -543,7 +543,7 @@ describe('processor', () => {

return expect(
processor(folderPath, '**/*.txt', async (data: any, filename: string) => Promise.reject('REJECT'), ProcessorDataFormat.String)
).rejects.toEqual('Metadata processor failed: REJECT');
).rejects.toEqual('Metadata processor failed: Failed to process: ' + path.join('folder1', 'foldera', 'text-file.txt') + '. Error: REJECT');
});

it('processing async function rejects (object) - error is caught and processor rejects', () => {
Expand All @@ -552,6 +552,6 @@ describe('processor', () => {

return expect(
processor(folderPath, '**/*.txt', async (data: any, filename: string) => Promise.reject(new Error('REJECT')), ProcessorDataFormat.String)
).rejects.toEqual('Metadata processor failed: REJECT');
).rejects.toEqual('Metadata processor failed: Failed to process: ' + path.join('folder1', 'foldera', 'text-file.txt') + '. Error: REJECT');
});
});

0 comments on commit feef3ef

Please sign in to comment.