DEV Community

Cover image for Complementing exceptions - Introducing monads for error handling in ruby

Complementing exceptions - Introducing monads for error handling in ruby

Cherry Ramatis on August 13, 2023

Have you ever considered your approach to handling exceptions? I'm referring to the method in which you use the raise keyword within a class, and t...
Collapse
 
topofocus profile image
Hartmut B.

Thanks for this comprehensive summary.
I looked at ChatGBT for DRY::Monads. The answer focuses mostly on aspects of functional programming. Then I understood, why you used the unusual A.new.call approach in your examples.
I translated your first example to (to me) familar ruby

class Auth
  extend Dry::Monads[:result]
  def self.authenticate name
      name == 'correct' ?  Success( name: name) : Failure( error: "No such User") 
  end
end

r= A.authenticate( 'somone' ) 
r.success?   

Enter fullscreen mode Exit fullscreen mode

Now the approach behaves much more like a state machine then a variation of error-handling.

Collapse
 
topofocus profile image
Hartmut B.

A second observation.

your example

class Runner
  include Dry::Monads::Do.for(:call)

  def call
    value = Auth.new.authenticate(name: 'incorrect')

    return value.failure if value.failure?

    yield value
  end
end
Enter fullscreen mode Exit fullscreen mode

does work. However, the returnstatement is not needed.
If you try the example without it and run into Failure, the statement Runner.new.callsimply returns the Dry::Monad::Result::Failure object.
Check it out!

Collapse
 
cherryramatis profile image
Cherry Ramatis

I honestly prefer the return for legibility reason, but it's cool if we don't need it

Collapse
 
cherryramatis profile image
Cherry Ramatis

Amazing incrementing to this article! Thanks a lot

Collapse
 
it4lo_dev profile image
Italo

Qualidade demais Cherry👏👏👏

Collapse
 
cherryramatis profile image
Cherry Ramatis

Muito obrigada primo!

Collapse
 
canhassi profile image
Canhassi

muito brabo!

Collapse
 
renanvidal profile image
Renan Vidal Rodrigues

Excellent article! It's always amazing to read!

Collapse
 
cherryramatis profile image
Cherry Ramatis

Thanks for the kind words cousin <3

Collapse
 
dantas profile image
Gustavo

Didática e conteúdos muito bons!!!! Manda demais prima!!

Collapse
 
cherryramatis profile image
Cherry Ramatis

Mto obrigada primo <3

Collapse
 
respect17 profile image
Kudzai Murimi

Well Explained hey ! Excellent

Collapse
 
cherryramatis profile image
Cherry Ramatis

Thanks a lot 🩷

Collapse
 
jvictorv profile image
João Victor

Faço um uso parecido no TS com o effect, fica lindo demais

Collapse
 
cherryramatis profile image
Cherry Ramatis

Uma das inspirações foram essas implementações no typescript, não conheço explicitamente o effect, mas realmente funciona super bem

Collapse
 
samucadev profile image
SamucaDev

Amazing, the queen of vim!

Collapse
 
cherryramatis profile image
Cherry Ramatis

Thanks a lot cousin <3

Collapse
 
ilonavarro profile image
Ilo Navarro

Show!

Collapse
 
foxgeeek profile image
Foxgeeek

Cualidad!

Collapse
 
cherryramatis profile image
Cherry Ramatis

ty <3

Collapse
 
juanboice profile image
JuanBoice

Excellent article. Thanks a good article shared with us. witchcraft love spells

Collapse
 
zoldyzdk profile image
Hewerton Soares

Sá salvei pra ler mais tarde, muito bom o conteúdo!!

Collapse
 
cherryramatis profile image
Cherry Ramatis

Muito obrigada primo

Collapse
 
marcelomagal profile image
oChefDev

Nice article.

Collapse
 
cherryramatis profile image
Cherry Ramatis

Ty <3

Collapse
 
phenriquesousa profile image
Pedro Henrique

Cherry... você é sensacional, prima <3

Collapse
 
cherryramatis profile image
Cherry Ramatis

O que eu puder fazer por todo mundo é sempre um prazer, brigada pelo apoio primo