From c011fdc8ae5c19a061868e9579236f34917e6ff1 Mon Sep 17 00:00:00 2001 From: Dialpuri Date: Wed, 5 Jun 2024 11:17:53 +0100 Subject: [PATCH] Add function to fetch PDB file if CIF file fails Introduced a fallback function, fetchPDBFile, that attempts to fetch a PDB file in case fetching the CIF file fails. This function catches networking errors and provides clearer logging to facilitate troubleshooting. It also improves the error handling for existing fetchPDB function. --- webapp/src/utils/fetch_from_pdb.ts | 34 +++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/webapp/src/utils/fetch_from_pdb.ts b/webapp/src/utils/fetch_from_pdb.ts index 98a3ccfa..ed25082f 100644 --- a/webapp/src/utils/fetch_from_pdb.ts +++ b/webapp/src/utils/fetch_from_pdb.ts @@ -1,9 +1,32 @@ +async function fetchPDBFile(PDBCode: string): Promise { + console.warn( + 'The CIF file for this PDB could not be found, trying for the PDB' + ); + const pdbURL = `https://files.rcsb.org/download/${PDBCode.toUpperCase()}.pdb`; + + const file = fetch(pdbURL) + .then(async (response) => { + if (!response.ok) { + throw new Error('Network error'); + } + return await response.text(); + }) + .then(async (file) => { + return file; + }) + .catch(async (error) => { + return await Promise.reject(error); + }); + return await file; +} + export async function fetchPDB(PDBCode: string): Promise { if (PDBCode == null) { return; } + // first try fetching the cif console.log('Fetching PDB ', PDBCode); - const pdbURL = `https://files.rcsb.org/download/${PDBCode.toUpperCase()}.pdb`; + const pdbURL = `https://files.rcsb.org/download/${PDBCode.toUpperCase()}.cif`; const file = fetch(pdbURL) .then(async (response) => { @@ -15,8 +38,13 @@ export async function fetchPDB(PDBCode: string): Promise { .then(async (file) => { return file; }) - .catch(() => { - throw new Error('PDB Not Found'); + .catch(async () => { + // if we can't find the cif, try the PDB, if that fails, then report the failure. + try { + return await fetchPDBFile(PDBCode); + } catch (e) { + return await Promise.reject(e); + } }); return await file; }