I've been looking for answer for this question and found this blog post. It says that fail
has advantages over error
because it isn't constrained to the IO
monad.
Should i use fail
instead of error
all over my code? Does fail
raise Control.Exception.catch
able exceptions in IO
?
EDIT: I've found an update for the link above.
Both failure and error in JUnit tests indicate an undesired situation, but their semantics are different. Failures notify of an invalid test result, errors indicate an unexpected test execution. Also, please check out the example code at GitHub.
Mistakes are an accident. You know it's wrong, but the wrong word slips out. An error, on the other hand, is something you don't know. It's grammar you haven't learned yet or vocabulary you haven't learned the nuance of yet.
Testing is the process of identifying defects, where a defect is any variance between actual and expected results. “A mistake in coding is called Error, error found by tester is called Defect, defect accepted by development team then it is called Bug, build does not meet the requirements then it Is Failure.”
An error is a mistake. If the mistake reaches the user, it is a fault. The fault is the cause of a failure. It's called defect if it's found during development time, bug when it's found while testing and failure when the user finds it.
Here's my advice:
If you're in IO
code already, use proper exceptions via throwIO
from Control.Exception
.
If your code uses a monad stack already, add errors to your monad stack if it doesn't support it already, and use those.
If you are in non-monadic code, write total functions. I.e., avoid error
and incomplete patterns if you can. Using fail
here would only force your code to be unnecessarily monadic. If you need exceptional results, use a proper data type (such as Maybe
or Either
or a custom datatype).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With