Skip to content

Commit

Permalink
fix(components): gs-wastewater-mutations-over-time: throw proper erro…
Browse files Browse the repository at this point in the history
…r when there is an invalid mutation (#687)

Otherwise, there would just be a type error complaining about `null` somewhere.
  • Loading branch information
fengelniederhammer authored Jan 27, 2025
1 parent ec3cc1e commit 3b98625
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 17 deletions.
30 changes: 29 additions & 1 deletion components/src/query/queryWastewaterMutationsOverTime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,35 @@ describe('queryWastewaterMutationsOverTime', () => {
);

await expect(queryWastewaterMutationsOverTime(DUMMY_LAPIS_URL, lapisFilter)).rejects.toThrowError(
/^Failed to parse mutation frequency/,
/Failed to parse mutation frequency/,
);
});

it('should error on invalid mutation', async () => {
const lapisFilter = { country: 'Germany' };

lapisRequestMocks.details(
{
country: 'Germany',
fields: ['date', 'location', 'nucleotideMutationFrequency', 'aminoAcidMutationFrequency'],
},
{
data: [
{
date: '2021-01-01',
location: 'Germany',
reference: 'organismA',
nucleotideMutationFrequency: JSON.stringify({
'not a mutation': 0.4,
}),
aminoAcidMutationFrequency: null,
},
],
},
);

await expect(queryWastewaterMutationsOverTime(DUMMY_LAPIS_URL, lapisFilter)).rejects.toThrowError(
/Failed to parse mutation: "not a mutation"/,
);
});
});
46 changes: 30 additions & 16 deletions components/src/query/queryWastewaterMutationsOverTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,26 @@ export async function queryWastewaterMutationsOverTime(
]);
const data = (await fetchData.evaluate(lapis, signal)).content;

return data.map((row) => ({
location: row.location as string,
date: toTemporalClass(parseDateStringToTemporal(row.date as string, 'day')),
nucleotideMutationFrequency:
row.nucleotideMutationFrequency !== null
? transformMutations(JSON.parse(row.nucleotideMutationFrequency as string))
: [],
aminoAcidMutationFrequency:
row.aminoAcidMutationFrequency !== null
? transformMutations(JSON.parse(row.aminoAcidMutationFrequency as string))
: [],
}));
return data.map((row) => {
try {
return {
location: row.location as string,
date: toTemporalClass(parseDateStringToTemporal(row.date as string, 'day')),
nucleotideMutationFrequency:
row.nucleotideMutationFrequency !== null
? transformMutations(JSON.parse(row.nucleotideMutationFrequency as string))
: [],
aminoAcidMutationFrequency:
row.aminoAcidMutationFrequency !== null
? transformMutations(JSON.parse(row.aminoAcidMutationFrequency as string))
: [],
};
} catch (e) {
throw new Error(
`Failed to parse row of wastewater data: ${JSON.stringify(row)}: ${(e as Error)?.message ?? 'Unknown error'}`,
);
}
});
}

const mutationFrequencySchema = z.record(z.number().nullable());
Expand All @@ -48,8 +56,14 @@ function transformMutations(input: unknown): { mutation: Substitution; proportio
throw new Error(`Failed to parse mutation frequency: ${mutationFrequency.error.message}`);
}

return Object.entries(mutationFrequency.data).map(([key, value]) => ({
mutation: SubstitutionClass.parse(key)!,
proportion: value,
}));
return Object.entries(mutationFrequency.data).map(([key, value]) => {
const mutation = SubstitutionClass.parse(key);
if (mutation === null) {
throw new Error(`Failed to parse mutation: "${key}"`);
}
return {
mutation,
proportion: value,
};
});
}

0 comments on commit 3b98625

Please sign in to comment.