Skip to content

Commit def4e91

Browse files
committed
fix: add try catch for errors for template csv download
1 parent ce3afb2 commit def4e91

2 files changed

Lines changed: 40 additions & 26 deletions

File tree

apps/web/src/features/upload/pages/UploadPage.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,18 @@ export const UploadPage = () => {
2727
const { t } = useTranslation();
2828

2929
const handleTemplateDownload = () => {
30-
const { content, fileName } = createUploadTemplateCSV(instrument!);
31-
void download(fileName, content);
30+
try {
31+
const { content, fileName } = createUploadTemplateCSV(instrument!);
32+
void download(fileName, content);
33+
} catch (error) {
34+
addNotification({
35+
message: t({
36+
en: 'Error occurred downloading sample template',
37+
fr: "Un occurence d'un erreur quand le csv document est telecharger"
38+
}),
39+
type: 'error'
40+
});
41+
}
3242
};
3343

3444
const handleInstrumentCSV = async () => {

apps/web/src/features/upload/utils.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -411,40 +411,44 @@ function generateSampleData({
411411

412412
export function createUploadTemplateCSV(instrument: AnyUnilingualFormInstrument) {
413413
// TODO - type validationSchema as object
414-
const instrumentSchema = instrument.validationSchema as z.AnyZodObject;
414+
try {
415+
const instrumentSchema = instrument.validationSchema as z.AnyZodObject;
415416

416-
const instrumentSchemaDef: unknown = instrument.validationSchema._def;
417+
const instrumentSchemaDef: unknown = instrument.validationSchema._def;
417418

418-
let shape: { [key: string]: z.ZodTypeAny } = {};
419+
let shape: { [key: string]: z.ZodTypeAny } = {};
419420

420-
if (isZodTypeDef(instrumentSchemaDef) && isZodEffectsDef(instrumentSchemaDef)) {
421-
const innerSchema: unknown = instrumentSchemaDef.schema._def;
422-
if (isZodTypeDef(innerSchema) && isZodObjectDef(innerSchema)) {
423-
shape = innerSchema.shape() as { [key: string]: z.ZodTypeAny };
421+
if (isZodTypeDef(instrumentSchemaDef) && isZodEffectsDef(instrumentSchemaDef)) {
422+
const innerSchema: unknown = instrumentSchemaDef.schema._def;
423+
if (isZodTypeDef(innerSchema) && isZodObjectDef(innerSchema)) {
424+
shape = innerSchema.shape() as { [key: string]: z.ZodTypeAny };
425+
}
426+
} else {
427+
shape = instrumentSchema.shape as { [key: string]: z.ZodTypeAny };
424428
}
425-
} else {
426-
shape = instrumentSchema.shape as { [key: string]: z.ZodTypeAny };
427-
}
428429

429-
const columnNames = Object.keys(shape);
430+
const columnNames = Object.keys(shape);
430431

431-
const csvColumns = INTERNAL_HEADERS.concat(columnNames);
432+
const csvColumns = INTERNAL_HEADERS.concat(columnNames);
432433

433-
const sampleData = [...INTERNAL_HEADERS_SAMPLE_DATA];
434-
for (const col of columnNames) {
435-
const typeNameResult = getZodTypeName(shape[col]!);
436-
if (!typeNameResult.success) {
437-
throw new Error(typeNameResult.message);
434+
const sampleData = [...INTERNAL_HEADERS_SAMPLE_DATA];
435+
for (const col of columnNames) {
436+
const typeNameResult = getZodTypeName(shape[col]!);
437+
if (!typeNameResult.success) {
438+
throw new Error(typeNameResult.message);
439+
}
440+
sampleData.push(generateSampleData(typeNameResult));
438441
}
439-
sampleData.push(generateSampleData(typeNameResult));
440-
}
441442

442-
unparse([csvColumns, sampleData]);
443+
unparse([csvColumns, sampleData]);
443444

444-
return {
445-
content: unparse([csvColumns, sampleData]),
446-
fileName: `${instrument.internal.name}_${instrument.internal.edition}_template.csv`
447-
};
445+
return {
446+
content: unparse([csvColumns, sampleData]),
447+
fileName: `${instrument.internal.name}_${instrument.internal.edition}_template.csv`
448+
};
449+
} catch {
450+
throw new Error('Error generating Sample CSV template');
451+
}
448452
}
449453

450454
export async function processInstrumentCSV(

0 commit comments

Comments
 (0)