Skip to content

Commit f84a3bc

Browse files
committed
Improve error handling on missing config
The check is done closer to where the data is actually used. Closes #76
1 parent 1dc2640 commit f84a3bc

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

lib/pusher.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ module Pusher
1717
# end
1818
class Error < RuntimeError; end
1919
class AuthenticationError < Error; end
20-
class ConfigurationError < Error; end
20+
class ConfigurationError < Error
21+
def initialize(key)
22+
super "missing key `#{key}' in the client configuration"
23+
end
24+
end
2125
class HTTPError < Error; attr_accessor :original_error; end
2226

2327
class << self

lib/pusher/client.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,14 @@ def initialize(options = {})
4040

4141
# @private Returns the authentication token for the client
4242
def authentication_token
43+
raise ConfigurationError, :key unless @key
44+
raise ConfigurationError, :secret unless @secret
4345
Pusher::Signature::Token.new(@key, @secret)
4446
end
4547

4648
# @private Builds a url for this app, optionally appending a path
4749
def url(path = nil)
50+
raise ConfigurationError, :app_id unless @app_id
4851
URI::Generic.build({
4952
:scheme => @scheme,
5053
:host => @host,
@@ -186,7 +189,6 @@ def webhook(request)
186189
# should not contain anything other than letters, numbers, or the
187190
# characters "_\-=@,.;"
188191
def channel(channel_name)
189-
raise ConfigurationError, 'Missing client configuration: please check that key, secret and app_id are configured.' unless configured?
190192
Channel.new(url, channel_name, self)
191193
end
192194

spec/client_spec.rb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@
5858
it "should fail on bad urls" do
5959
expect { @client.url = "gopher/somekey:somesecret@://api.staging.pusherapp.co://m:8080\apps\87" }.to raise_error(URI::InvalidURIError)
6060
end
61+
62+
it "should raise exception if app_id is not configured" do
63+
@client.app_id = nil
64+
expect {
65+
@client.url
66+
}.to raise_error(Pusher::ConfigurationError)
67+
end
6168
end
6269

6370
describe 'configuring the cluster' do
@@ -116,13 +123,11 @@
116123
expect(@channel).to be_kind_of(Pusher::Channel)
117124
end
118125

119-
%w{app_id key secret}.each do |config|
120-
it "should raise exception if #{config} not configured" do
121-
@client.send("#{config}=", nil)
122-
expect {
123-
@client['test_channel']
124-
}.to raise_error(Pusher::ConfigurationError)
125-
end
126+
it "should raise exception if app_id is not configured" do
127+
@client.app_id = nil
128+
expect {
129+
@client['test_channel']
130+
}.to raise_error(Pusher::ConfigurationError)
126131
end
127132
end
128133

0 commit comments

Comments
 (0)