Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

elm
communitycreator

What is ELM error handling?

Nouman Mehmood

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

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.

//Syntax
Name_of_variable:MayBe data_type
//Declearing a maybe variable and assigning value to it
User_Name : Maybe String
User_Name= Just "Jhon"
//Declaring a MayBe variable and assigning value to it
User_Age : Maybe Int
User_Age = Just 25
//Declaring a MayBe variable and assigning value to it
User_Name:Maybe String
User_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.

//Syntax
type 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.

RELATED TAGS

elm
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring