Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document that PhylogenyWrapper.getNodeLabels() and .getParsedNewick() may throw an Error #116

Open
gaurav opened this issue Mar 28, 2022 · 3 comments

Comments

@gaurav
Copy link
Member

gaurav commented Mar 28, 2022

PhylogenyWrapper.getNodeLabels() and PhylogenyWrapper.getParsedNewick() currently use the parse method in newick-js to parse Newick strings. See also PhylogenyWrapper.getParsedNewickWithIRIs(), which by default uses getParsedNewick() to parse the Newick.

const { graph } = parseNewick(this.phylogeny.newick || '()');

const { graph, root, rootWeight } = parseNewick(newick);

If the Newick string is malformed in particular ways (such as if they're empty), this will throw an Error, which at the moment we propagate to the code calling us. We should document this so that code that calls us knows to check for an exception.

One (slow) way of checking this is to run PhylogenyWrapper.getErrorsInNewickString(), which does wrap parse() -- so you can test whether a Newick string if valid for newick-js/phyx.js purposes by checking to see if this method returns an empty list before using either of the two methods described above. But that's probably unnecessarily complicated for most uses.

parseNewick(newickTrimmed);

@hlapp
Copy link
Member

hlapp commented Apr 8, 2022

I agree, it sounds like this is what exceptions and try {} catch {} blocks are for.

@gaurav
Copy link
Member Author

gaurav commented May 24, 2023

I was hoping I could close this just by adding a @throws to the documentation to let everybody know that this function might throw an exception, but in JavaScript -- unlike languages like Java -- there's no way to make sure that code calling this function traps any generated exception. This is particularly bad when running this code in a browser, where a thrown exception would stop the thread.

After thinking about it some more, I think a better solution might be to return a Promise -- that way, calling code could simply use .then(success) to silently ignore any errors, or .then(success).catch(error) if they can do something with the error. Since it's parseNewick() that actually throws the exception, we should create a single static method that parses Newick and returns a Promise; all functions that use parseNewick() would then use that method and return Promises themselves. This would guarantee that we never accidentally forget to handle Newick parsing errors, but at the cost of more complex code.

What do you think?

@gaurav gaurav added this to the Phyx.js v1.1.0 milestone May 24, 2023
@hlapp
Copy link
Member

hlapp commented May 24, 2023

in JavaScript -- unlike languages like Java -- there's no way to make sure that code calling this function traps any generated exception

Yes, but as you indicate this is a feature of the language. (Similarly, there is no type checking, and thus no way to somehow enforce that a calling client passes the correct types as arguments to a method. Arguably this, too, is a feature of the language, even if some believe it's a bad one.)

Point being, I don't know why we should feel obligated or even only inclined to try to somehow work around JavaScript's language feature where they differ from other languages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants