Tuesday, June 16, 2020

Say Bye Bye to Try Catch(object orientation method).... Handle Error in Pure Functional Way...


Functional Error Handling in Scala



import scala.util.Try
package ErrorHandling

***********Section1************************************************
This code will handle the error but this is not the pure functional way
we are breaking functional purity. We are not returning the same type always

object worksheet_FPStyleErrorHandling extends App {
case class Person(name:String,location:String) val person = Person("Divyanshu","Bangalore") def getPerson(person: Person):Person ={ if(person.location.equals("Bangalore")){ person } else { throw new Exception("Invalid object") } } val requiredPerson = getPerson(person) println(requiredPerson) } ********************Section2*********************************************
Pure Functional Way
First method: Option
Options handle both sides of the coin i.e both positive and negative.
It uses Some(value) for the positive case and None for the negative case.

object optionMethod extends App {

  case class Person1(name: String, location: String)

  val person1 = Person1("Divyanshu", "Mumbai")

 def first(person1: Person1): Option[Person1]= {
   if (person1.location.equals("Mumbaif")) {
     Some(person1)
   }
   else {
     None
   }
 }
   val requiredPerson = first(person1)
  println(requiredPerson)
}

*****************Section 3*************************************
Second Way : Try
Try is another technique to achieve functional purity. Try results in Success(value)
 or Failure(exception).Try is ideal when we are dealing with third-party libraries.
Try/Success/Failure is commonly used when writing methods that interact with files,
databases, and internet services.

object trymethod extends App{
case class Person2(name:String,location:String)
val person2 = Person2("Divyanshu","Delhi")
  def second(person2: Person2):Try[Person2] = {
    Try{
   if (person2.location.equals(4)){
     person2
   }
      else{
      throw new Exception("invalid Person")
   }
    }
  }
val requiredPersonnew = second(person2)
  println(requiredPersonnew)
}

****************Section4*******************************
Either:
Either also handles both cases as other techniques do but it is better than Option and Try.
 Option has a demerit that it does not provide the error message in case of failures, 
it just returns None for the failures. Try provides us a failure message for a failure
 but still, we need to throw an exception which we want to avoid. Either is a good 
approach to avoid these. It returns Left or Right. Left contains the error message
 that we pass in our code and Right contains the value itself.

object Either extends App {

  case class Person3(name: String, location: String)

  case class ErrorMessage(message: String)

  val person3 = Person3("Divyanshu", "UP")

  def Eithermethod(person3: Person3): Either[ErrorMessage, Person3] = {
    if (person3.location.equals("UP9")) {
      Right(person3)
    }
    else {
      Left(ErrorMessage("invalid object"))
    }
  }

  val requiredpersonEither = Eithermethod(person3)
  println(requiredpersonEither)
}

No comments:

Post a Comment