What is ELM error handling?
ELM error handling
When we do code in ELM, we will not see runtime errors. This is because ELM treats errors as data. Rather than crashing, we replicate the chance of failure expressed with a custom type. ELM handles the error with the types
MayBe and
Result.
The MayBe error
The easiest way to represent an error in ELM is to alimony a MayBe type on the model that contains an Error type. Syntax and the following example show us how to use the MayBe type with variables and functions.
//SyntaxName_of_variable:MayBe data_type
//Declearing a maybe variable and assigning value to itUser_Name : Maybe StringUser_Name= Just "Jhon"//Declaring a MayBe variable and assigning value to itUser_Age : Maybe IntUser_Age = Just 25//Declaring a MayBe variable and assigning value to itUser_Name:Maybe StringUser_Name = Nothing
The Result error
The Result type should be used if the application wants to definitely raise an error and return the details about what went wrong.
Parameters
The Result type declaration takes two parameters: the data type of the error, usually a string, and the data type of the result to be returned. The syntax is given below.
//Syntaxtype Result Error_type Data_value_type = Ok Data_value| err error_message
Return value
The Result type returns the values as follows.
Ok: This is any value that represents the result to be returned.
Err: This represents the error message that will be returned if it is not satisfied with the expected condition.