amro/uakari — Issue #13: Raising exceptions on delivery (and JSON parsing of the response)
Author: scottley
Created: 2012-01-15 00:01:05+00:00
State: open

Hi Gang,

I'd like to have Uakari raise delivery exceptions if Mailchimp rejects the request. This way errors like unverified senders will bubble up. I have a few questions about your code:

```ruby
def call(method, params = {})
url = "#{base_api_url}#{method}"
params = @default_params.merge(params)
response = self.class.post(url, :body => params, :timeout => @timeout)

begin
response = JSON.parse(response.body)
rescue
response = JSON.parse('['+response.body+']').first
end

response
end
```
According to the docs, it seems like Httparty automatically generates a hash from the API response. The JSON parsing steps don't seem necessary, since I can already type:

```
response["status"]
=> "sent"
```

for example, without having to first parse it.

So, in order to handle error responses, I'm thinking of changing the code to read:

```ruby
def call(method, params = {})
url = "#{base_api_url}#{method}"
params = @default_params.merge(params)
response = self.class.post(url, :body => params, :timeout => @timeout)

if response["http_code"] && (response["http_code"] != 200)
raise MailchimpSTSApiError, response.body
end

response
end
```

If you don't want delivery errors to be raised, you can turn them off globally with

ActionMailer::Base.raise_delivery_errors = false

or by changing the value in your Rails environment settings:

config.action_mailer.raise_delivery_errors = false

It can also be set on a per message basis, since raise_delivery_errors is defined on Mail::Message :

message.raise_delivery_delivery_errors = false

I know folks might use Uakari without rails. I could add a setting to Uakari to support that, but I don't know enough about that use case to know what would work best.

What do you think?

-=Scott