Skip to content

Commit

Permalink
Raise an error when an empty tree is created
Browse files Browse the repository at this point in the history
Raise an error when `treeToNewick` creates an empty tree is created
because this is undesired behavior. Created a custom error class for
this so that we could include details in the warning notification for
the user.
  • Loading branch information
joverlee521 committed Nov 3, 2022
1 parent 79d3bb9 commit 4ff518c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/components/download/helperFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { numericToCalendar } from "../../util/dateHelpers";
import { NODE_VISIBLE } from "../../util/globals";
import { datasetSummary } from "../info/datasetSummary";
import { isColorByGenotype } from "../../util/getGenotype";
import { EmptyNewickTreeCreated } from "../../util/exceptions";

export const isPaperURLValid = (d) => {
return (
Expand Down Expand Up @@ -43,7 +44,11 @@ const treeToNewick = (tree, temporal, internalNodeNames=false, nodeAnnotation=()
*/
const rootNode = tree.nodes[tree.idxOfFilteredRoot || tree.idxOfInViewRootNode];
const rootXVal = getXVal(rootNode);
return recurse(rootNode, rootXVal) + ";";
const newickTree = recurse(rootNode, rootXVal);
if (!newickTree) {
throw new EmptyNewickTreeCreated();
}
return newickTree + ";";
};

const MIME = {
Expand Down Expand Up @@ -334,7 +339,11 @@ export const exportTree = ({dispatch, filePrefix, tree, isNewick, temporal, colo
dispatch(infoNotification({message: `${temporal ? "TimeTree" : "Tree"} written to ${fName}`}));
} catch (err) {
console.error(err);
dispatch(warningNotification({message: "Error saving tree!"}));
const warningObject = {message: "Error saving tree!"};
if (err instanceof EmptyNewickTreeCreated) {
warningObject.details = "An empty tree was created. If you have selected genomes, note that we do not support downloads of multiple subtrees.";
}
dispatch(warningNotification(warningObject));
}
};

Expand Down
12 changes: 12 additions & 0 deletions src/util/exceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,15 @@ export class NoContentError extends Error {
super(...params);
}
}

/**
* Thrown when a download produces an empty Newick tree.
* Usually caused by users trying to download multiple subtrees that do not
* share a common ancestor that is "visible"
*/
export class EmptyNewickTreeCreated extends Error {
constructor(...params) {
super(...params);
this.name = this.constructor.name;
}
}

0 comments on commit 4ff518c

Please sign in to comment.