Skip to content

Commit

Permalink
Add function to fetch PDB file if CIF file fails
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Dialpuri committed Jun 5, 2024
1 parent c9a50b2 commit c011fdc
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions webapp/src/utils/fetch_from_pdb.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
async function fetchPDBFile(PDBCode: string): Promise<string | void> {
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<string | void> {
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) => {
Expand All @@ -15,8 +38,13 @@ export async function fetchPDB(PDBCode: string): Promise<string | void> {
.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;
}
Expand Down

0 comments on commit c011fdc

Please sign in to comment.