I was using function Float.of_string to convert an input obtained by a formular.
Surely the user could be a bad guys and write a sentence instead of a float number in the input :)
There is lot of solution, to test the string input... But In my mind the function float.of_string, should t开发者_如何学Goest, it's obligatory to transform into a float. And as an evidence when the string is not a float... The server will reach an error which kill the server (or at minimum the session).
Then I think, the best will be (choice) :
- Return an option
- A way to catch error before it kill the session...
Then the true question of this post, is : Is there a way to do something like try/catch block for the OPA uncaught exception. And if yes how it works, and is it possible to throw an error in our own function ?
Thanks for your answer, Matthieu
Ok, first concerning Float.of_string
. It's a low-level function that does assume a well-formed input, so should not be called on anything coming from the user (or indeed anything that may fail). But you're not the first one asking that so maybe indeed we should change this function to return an option type.
For that kind of scenarios (when things can go wrong) one should instead use parsing and the Rule.float
function. To learn more about parsing in Opa you can check the recent blog post and there will be a follow-up one coming tomorrow, I hope.
Now, concerning exception handling: we were (are?) planning to add it to Opa but as of today it's not available yet. So... be patient :)
精彩评论