-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
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
fix mermaidAPI.parse() behavior to match documentation, add tests to ensure behavior matches docs #3004
Conversation
…d a parseError handler is defined))
I'm a bit conflicted on whether the documentation behaviour should be the correct one or not. The current behaviour somewhat fits the cognitive flow, Giving the ability to set a separate error handler will lead to more pathways to failures, race conditions, etc. (e.g.: Multiple error handlers being set in different places). Either the error handler should be passed as part of the parse call (but this would open up the possibility of callback hell), or the parse function should throw an error (current behaviour). If the function was named
Would this have to be released as a major version change? |
// Is this the correct way to access mermiad's parseError() | ||
// method ? (or global.mermaid.parseError()) ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My question here was because there are some places in the code that the parseError() method is called as
mermaid.parseError()
and a couple places where it is called as
global.mermaid.parseError()
These two should be identical, but it is more of a stylistic question for the mermaid code base.
To be clear the error is always still thrown, except when a The documented behavior is the most useful, as sure we could change the docs, or the name, that that seems way more 'breaking'. We could add an additional new validate() method that works as you describe if that is needed, but the parse() function would do everything the validate() did and you could ignore the return value. The problem for me is that throwing the exception does not help me (I can't detect it), I am using mermaid from a Dart<->JS interop wrapper.. Being able to set the parseError() callback you already have been able to do is much more versatile.
This remains the case unless you set a parseErrorI() handler, in which case the user gets to choose to re-throw or now.
The existing functionality has already had the ability to set
|
I was referring to the documented behaviour and current implementation, this PR is absolutely fine. |
Thanks for looking at this @timmaffett and thanks @sidharthv96 for your review. I will merge. Agree that it should be consistent in how parseError is called. I think it looks better to use mermaid.parseError instead of global.mermaid.parseError even though I suppose they would both work fine. |
This PR fixes the behavior of
mermaidAPI.parse()
to match to documentation hereIt now returns true (instead of the
parse
object !?) if the diagram has valid syntax.If there is no
mermaid.parseError
handler defined then it will still throw an error as before, however, if there is amermaid.parseError
handler defined then themermaid.parseError
handler will be called and then false will be returned.The behavior of requiring a
parseError()
handler to be defined is that so all existing tests (prior to this PR) still pass (ie. requiring a throw when parse() is called on invalid input).The documentation in newHandler.md for parseError is also corrected to match reality.
I had added tests to mermaidAPI.spec.js to test all the new code and that the behavior matches to documented behavior.
Within mermaid I have added a
setParseErrorHandler()
method that can be used instead ofmermaid.parseError = function()...
The reason for this is that I am using mermaid within a Dart interop wrapper, and it is not allowed to add a new member variable to an existing javascript object, and we start with mermaid NOT having any parseError member defined.
Within the mermaid export object definition I check for a undefined value of
mermaidAPI
before using it. The reason for this is that mermaidAPI is sometimes NOT defined and this was causing an error when mermaid.js was required within mermaidAPI.js/mermaidAPI.spec.js (wheremermaidAPI
was not yet defined).The only behavior changed by this PR is that
parse()
now returns true when the input is valid, and if there is amermaid.parseError
handler defined thenparse()
will now return false instead of throwing an exception.I verified that all existing (and new) test pass.
The existing behavior described in the existing documentation makes sense, so I simply made the code match that 'reality'.