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

markijbema avatar parndt avatar schneems avatar tvon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.