Skip to content

Commit 85eae86

Browse files
committed
bubble up error messages
1 parent 26da6bd commit 85eae86

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

src/processing/postprocess.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,22 @@ const enum POSTPROCESSING_STEP {
2424
ENCRYPTION = 'ENCRYPTION',
2525
}
2626

27-
type Step = {
28-
success: boolean;
27+
type StepPassed = {
28+
success: true;
2929
step: POSTPROCESSING_STEP;
3030
outputPath?: string;
3131
};
3232

33+
type StepFailed = {
34+
success: false;
35+
step: POSTPROCESSING_STEP;
36+
error: string;
37+
};
38+
39+
3340
export interface PostprocessFileResult {
3441
success: boolean;
35-
steps: Step[];
42+
steps: (StepPassed | StepFailed)[];
3643
outputPath?: string;
3744
}
3845

@@ -75,7 +82,7 @@ export async function postprocessFile({ config, inputPath, outputPath, options }
7582

7683
if (!encryptResult.success) {
7784
log(`[ERROR] Encryption failed: ${encryptResult.error} (code: ${encryptResult.code})`);
78-
result.steps.push({ success: false, step: POSTPROCESSING_STEP.ENCRYPTION });
85+
result.steps.push({ success: false, step: POSTPROCESSING_STEP.ENCRYPTION, error: encryptResult.error });
7986
}
8087
else result.steps.push({ success: true, step: POSTPROCESSING_STEP.ENCRYPTION, outputPath: encryptResult.outputPath });
8188
}
@@ -84,6 +91,11 @@ export async function postprocessFile({ config, inputPath, outputPath, options }
8491

8592
log("[INFO] Postprocessing complete.");
8693
if (result.steps.some(s => !s.success)) result.success = false;
87-
else if (result.steps.length > 0) result.outputPath = result.steps[result.steps.length - 1].outputPath; // set outputPath to last successful step outputPath
94+
else {
95+
const lastStepWithOutput = [...result.steps].reverse().find(s => s.success && 'outputPath' in s && s.outputPath);
96+
if (lastStepWithOutput && 'outputPath' in lastStepWithOutput) {
97+
result.outputPath = lastStepWithOutput.outputPath;
98+
}
99+
}
88100
return result;
89101
}

tests/processing/postprocess.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ describe('postprocess::encrypt', () => {
132132
const res = await postprocessFile({ config, inputPath: 'in.csv', outputPath: 'out.gpg' });
133133

134134
expect(res.success).toBe(false);
135-
expect(res.steps).toEqual([{ success: false, step: 'ENCRYPTION' }]);
135+
expect(res.steps).toEqual([{ success: false, step: 'ENCRYPTION', error: 'Recipient key not found' }]);
136136
expect(res.outputPath).toBeUndefined();
137137
expect(hoisted.encryptFileMock).toHaveBeenCalledTimes(1);
138138
});

0 commit comments

Comments
 (0)