Handle errors with cytoscape.js

寵の児 提交于 2020-01-14 14:19:23

问题


I am building a website where users will be inputting graph data, which is then rendered with cytoscape.js.

Given that this is backed by user data, there may be instances where they put in invalid data, specifically edges that have missing targets. I'd like to capture this data and present it to the user so they can fix the errors.

Currently, I get this error in the logs, but I'm unable to figure out how to capture the data.

Can not create edge `TableOfBooks>att` with nonexistant target `att`

I've tried wrapping the cytoscape constructor in a try...catch... block, but it doesn't appear to be catching the error.

try {
  var cy = cytoscape(...);
} catch(err) {
  console.log("ERROR: " + err);
}

How can I go about catching these errors?


回答1:


That's just a warning in the console. It is not an exception, of type Error. In general, Cytoscape does not throw exceptions for invalid graph JSON. It includes valid entries, it warns you about invalid ones, and it continues on so there's something for the dev to inspect.

Cytoscape has to appeal to several groups, including inexperienced scientists and experienced programmers. We've found scientists would give up too easily on the tool if they get an exception for little graph JSON mistakes. So exceptions are a nonstarter.

If you really want thrown exceptions, you could look into adding a togglable flag for whether warnings throw exceptions in a PR. As of today, all those messages go through error() or warning() in /util, so you could add throwing there.

Cytoscape does not do user input validation. That's generally up to the app to do, especially since it could contain business logic.



来源:https://stackoverflow.com/questions/46257042/handle-errors-with-cytoscape-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!