Skip to content

Commit 15cd5cc

Browse files
committed
Add option to disable exceptions
1 parent bd4d809 commit 15cd5cc

2 files changed

Lines changed: 27 additions & 12 deletions

File tree

lib/sendgrid/client.rb

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@ module SendGrid
44
class Client
55
attr_accessor :api_user, :api_key, :protocol, :host, :port, :url, :endpoint,
66
:user_agent
7-
attr_writer :adapter, :conn
7+
attr_writer :adapter, :conn, :raise_exceptions
88

99
def initialize(params = {})
10-
self.api_user = params.fetch(:api_user, nil)
11-
self.api_key = params.fetch(:api_key, nil)
12-
self.protocol = params.fetch(:protocol, 'https')
13-
self.host = params.fetch(:host, 'api.sendgrid.com')
14-
self.port = params.fetch(:port, nil)
15-
self.url = params.fetch(:url, protocol + '://' + host + (port ? ":#{port}" : ''))
16-
self.endpoint = params.fetch(:endpoint, '/api/mail.send.json')
17-
self.adapter = params.fetch(:adapter, adapter)
18-
self.conn = params.fetch(:conn, conn)
19-
self.user_agent = params.fetch(:user_agent, "sendgrid/#{SendGrid::VERSION};ruby")
10+
self.api_user = params.fetch(:api_user, nil)
11+
self.api_key = params.fetch(:api_key, nil)
12+
self.protocol = params.fetch(:protocol, 'https')
13+
self.host = params.fetch(:host, 'api.sendgrid.com')
14+
self.port = params.fetch(:port, nil)
15+
self.url = params.fetch(:url, protocol + '://' + host + (port ? ":#{port}" : ''))
16+
self.endpoint = params.fetch(:endpoint, '/api/mail.send.json')
17+
self.adapter = params.fetch(:adapter, adapter)
18+
self.conn = params.fetch(:conn, conn)
19+
self.user_agent = params.fetch(:user_agent, "sendgrid/#{SendGrid::VERSION};ruby")
20+
self.raise_exceptions = params.fetch(:raise_exceptions, true)
2021
yield self if block_given?
2122
end
2223

@@ -38,7 +39,7 @@ def send(mail)
3839
req.body = payload
3940
end
4041

41-
fail SendGrid::Exception, res.body if res.status != 200
42+
fail SendGrid::Exception, res.body if raise_exceptions? && res.status != 200
4243

4344
SendGrid::Response.new(code: res.status, headers: res.headers, body: res.body)
4445
end
@@ -54,5 +55,9 @@ def conn
5455
def adapter
5556
@adapter ||= Faraday.default_adapter
5657
end
58+
59+
def raise_exceptions?
60+
@raise_exceptions
61+
end
5762
end
5863
end

spec/lib/sendgrid/client_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,15 @@
6969

7070
expect {client.send(mail)}.to raise_error(SendGrid::Exception)
7171
end
72+
73+
it 'should not raise a SendGrid::Exception if raise_exceptions is disabled' do
74+
stub_request(:any, 'https://api.sendgrid.com/api/mail.send.json')
75+
.to_return(body: {message: 'error', errors: ['Bad username / password']}.to_json, status: 400, headers: {'X-TEST' => 'yes'})
76+
77+
client = SendGrid::Client.new(api_user: 'foobar', api_key: 'abc123', raise_exceptions: false)
78+
mail = SendGrid::Mail.new
79+
80+
expect {client.send(mail)}.not_to raise_error
81+
end
7282
end
7383
end

0 commit comments

Comments
 (0)