Code Monkey home page Code Monkey logo

rrrretry's Introduction

rrrretry

Help Contribute to Open Source

Because retry was already taken.

Install

In your Gemfile add:

gem 'rrrretry'

Then run bundle install.

What Does it Do?

Monkey patches Enumerable to add a method called retry that attempts to run code for every element, returns the first valid run.

If code does not return, the original error will be raised.

Whoa Crazy! Give me an example:

Most people will use it for network communication like this:

10.times.retry do
  SomeNetworkCommunicationThingyHere.new
end

But if you prefer runnable examples check these out:

First run returns a divided by 0 error, so the next result is returned.

[0, 1, 2].each.retry { |i| 1/i }
  # => 1

If there are no valid returns, the last error will be raised.

[0, 1, 2].each.retry { raise "bar" }
  # => RuntimeError: bar

By default only StandardError is caught, multiple error types can be specified.

array = [->{ raise Exception }, ->{ raise SignalException }, ->{ 1 }]
array.each.retry(Exception, SignalException) { |i| i.call }
  # => 1

For the sake of reporting, previous exceptions are passed to the block.

[0, 1].each.retry do |i, ex|
  puts "LOG: Retrying due to exception: #{ex.to_s}" unless ex.nil?
  raise "bar"
end
  # => LOG: Retrying due to exception: bar
  # => RuntimeError: bar

Is it Complex?

Naw, it's 8 lines of ruby:

def retry(*exceptions, &block)
  exceptions << StandardError if exceptions.empty?
  enum       ||= self.to_enum
  yield enum.next
rescue *exceptions => e
  last_exception = e and retry unless e.is_a? StopIteration
  raise last_exception unless last_exception.nil?
end

Why?

Because I needed this code in more than one project, I didn't like the other solutions, and because coding is fun.

Isn't Monkey Patching Evil?

Yes. Do as I say, not as I do.

License

MIT YO. See MIT-LICENSE for more info.

rrrretry's People

Contributors

schneems avatar markijbema avatar parndt avatar tvon avatar

Stargazers

 avatar Andy Maleh avatar Ivan Nemytchenko avatar Marko Delic avatar Tommaso Barbato avatar Samuel Lourenço avatar Angus H. avatar Zsombor Szántó avatar Matt Morris avatar Thomas Klemm avatar David Elliott avatar George Masgras avatar Chris Johnson avatar Yazin avatar Tom Hensel avatar Mika Cohen avatar Tiago Bastos avatar Bruno Costa - Garu avatar Florian Bertholin avatar Vanja Radovanović avatar Boris Köster avatar Steve avatar Takamichi Yoshikawa avatar Josh Rieken avatar Klaus Hartl avatar Martin Samami ッ avatar Kirsten Comandich avatar Martin Tithonium avatar Emil Soman avatar Artem Kholodnyi avatar Leo Correa avatar Varun Rau avatar ZhouQiang avatar Phillip Reichelt avatar Arthur Nogueira Neves avatar  avatar Carina C. Zona avatar Ivan Kotelnikov avatar Alex Gorkunov avatar  avatar Geoff Tidey avatar David Underwood avatar Jason Coene avatar Michael May avatar Jonathon Jones avatar Jonathan Matthews avatar Vicente Reig avatar Matt Huggins avatar Huiming Teo avatar Patrick Schmitz avatar Adam Nowak avatar Chris Kelner avatar Kyle Geske avatar Kevin Hutson avatar Michel Pigassou avatar Jason Weathered avatar Amir Friedman avatar Adam Michela avatar Baris Gumustas avatar Sam Soffes avatar John F. Douthat avatar Ivan Kataitsev avatar Vicente Mundim avatar Joe Rafaniello avatar Philip H. MacIver avatar Michael Gee avatar Corey Reece avatar Jonathan Tron avatar Sean Huber avatar Justin Mazzi avatar Felipe Coury avatar Pritesh avatar

Watchers

James Cloos avatar Artem Kholodnyi avatar  avatar

rrrretry's Issues

Tricky special case for StopIteration

Hey,

I arrived on your repo via your blogpost on retrying, so disclaimer, I haven't tested this.

It seems like you might get some very obscure errors when your retried code fails with StopIteration. It looks like the retry will fail, but instead of throwing will return nil. i think you might want to consider testing whether you're on the last iteration, instead of using exceptions for this control flow.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.