How to catch a bug

I used the Float.of_string function to convert the input received by the formal.

Of course, the user can be a bad guy and write a sentence instead of a floating point number in the input :)

There are many solutions to check the input of a string ... But, in my opinion, the function float.of_string, should check, is required to convert to a float. And as proof that the string is not a float ... The server will achieve an error that kills the server (or at least the session).

Then I think the best would be (choice):

  • Return option
  • A way to catch a mistake before he kills a session ...

Then the true question of this post is: Is there a way to do something like a try / catch block for an OPA thrown exception. And if so, how does it work, and is it possible to issue an error in our own function?

Thank you for your reply, Mathieu.

+4
source share
1 answer

Ok, first regarding Float.of_string . This is a low-level function that assumes correct input, so you should not name anything that comes from the user (or, something that might not work). But you are not the first to ask what, perhaps, indeed, we should change this function in order to return the parameter type.

For such scenarios (when things may go wrong), you should use parsing and the Rule.float function Rule.float . To learn more about parsing in Opa, you can check out a recent blog post , and I hope the following answer appears tomorrow.

Now, regarding exception handling: we were (is it?) Planning to add it to Opa, but today it is not yet available. So ... be patient :)

+3
source

All Articles