Skip to content

Raise on some HTTP status codes#455

Merged
jnunemaker merged 1 commit intojnunemaker:masterfrom
dccunha:raise_on_option
Jan 4, 2016
Merged

Raise on some HTTP status codes#455
jnunemaker merged 1 commit intojnunemaker:masterfrom
dccunha:raise_on_option

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