Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise on some HTTP status codes #455

Merged
merged 1 commit into from
Jan 4, 2016

Conversation

dccunha
Copy link

@dccunha dccunha commented Dec 29, 2015

What?

Implements the proposal #454

Why?

When you use httparty in many different projects it starts to become tedious to add a verification on every request. To accomplish this, you have to do something like this:

module Api
  class Base
    include HTTParty

    def get(path, options = {})
      raises_unless_success { self.class.get path, options }
    end

    private

     def raises_unless_success
      httparty = yield
      fail HTTParty::Error, "Code #{httparty.code} - #{httparty.body}" unless
        httparty.response.is_a?(Net::HTTPSuccess)
      httparty
    end

  end
end

I believe this is not a straight solution.

My idea

module Api
  class Base
    include HTTParty
    raise_on (400..599).to_a

    def get(path, options = {})
      self.class.get path, options
    end
  end
end

@jnunemaker
Copy link
Owner

I actually don't like automatic raising of exceptions and prefer explicitly checking errors in each case, but I have a feeling that many users of httparty will like this and it is optional, so I'm going to merge. Thanks for the PR!

jnunemaker pushed a commit that referenced this pull request Jan 4, 2016
Raise on some HTTP status codes
@jnunemaker jnunemaker merged commit d85e617 into jnunemaker:master Jan 4, 2016
@dccunha dccunha deleted the raise_on_option branch January 20, 2016 12:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants