Skip to content

Commit

Permalink
improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
fhennig authored and corneliusroemer committed Oct 17, 2024
1 parent 7c19eab commit 6aad7a7
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions website/src/utils/createDownloadAPIRoute.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { APIRoute } from 'astro';
import { err, type Result } from 'neverthrow';
import { err, ok, type Result } from 'neverthrow';

import { parseAccessionVersionFromString } from './extractAccessionVersion';
import { getConfiguredOrganisms } from '../config';
Expand Down Expand Up @@ -128,24 +128,20 @@ const getSequenceData = async (
fileSuffix: string,
undefinedVersionRedirectUrl: RedirectRoute,
getter: DataDownloader,
) => {
): Promise<Result<Data | Redirect, string>> => {
// We don't know which organism the accessionVersion belongs to,
// so we just try all of them until we get a success.
const organisms = getConfiguredOrganisms();
const results = await Promise.all(
organisms.map((organism) =>
getSequenceDataWithOrganism(
accessionVersion,
organism.key,
fileSuffix,
undefinedVersionRedirectUrl,
getter,
),
const promises = organisms.map(({ key }) =>
getSequenceDataWithOrganism(accessionVersion, key, fileSuffix, undefinedVersionRedirectUrl, getter).then(
(result) => (result.isOk() ? ok(result.value) : Promise.reject()),
),
);
const firstSuccess = results.find((result) => result.isOk());
if (firstSuccess) {

try {
const firstSuccess = await Promise.any(promises);
return firstSuccess;
} catch (error) {
return err('Could not find accessionVersion in any organism.');
}
return results[0];
};

0 comments on commit 6aad7a7

Please sign in to comment.