Skip to content

Commit

Permalink
Raise clearer errors when configs are Missing (#48)
Browse files Browse the repository at this point in the history
e.g. currently if SNOWFLAKE_PRIVATE_KEY and SNOWFLAKE_PRIVATE_KEY_PATH
are not set, the error message is:
```TypeError: no implicit conversion of nil into String```

Also: Update Gemfile.lock. It had gotten outdated.
  • Loading branch information
jordan-brough authored Aug 5, 2024
1 parent 9ba8b75 commit 80ae8f7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
10 changes: 2 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
rb_snowflake_client (1.1.0)
rb_snowflake_client (1.1.1)
concurrent-ruby (>= 1.2)
connection_pool (>= 2.4)
dotenv (>= 2.8)
Expand Down Expand Up @@ -50,17 +50,11 @@ PLATFORMS

DEPENDENCIES
bundler
concurrent-ruby
connection_pool
dotenv
jwt
oj
parallel
pry
rake
rb_snowflake_client!
retryable
rspec

BUNDLED WITH
2.5.4
2.5.10
18 changes: 13 additions & 5 deletions lib/ruby_snowflake/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def initialize(details)
class BadResponseError < Error ; end
class ConnectionError < Error ; end
class ConnectionStarvedError < Error ; end
class MissingConfig < Error ; end
class RetryableBadResponseError < Error ; end
class RequestError < Error ; end
class QueryTimeoutError < Error ; end
Expand Down Expand Up @@ -73,14 +74,21 @@ def self.from_env(logger: DEFAULT_LOGGER,
thread_scale_factor: env_option("SNOWFLAKE_THREAD_SCALE_FACTOR", DEFAULT_THREAD_SCALE_FACTOR),
http_retries: env_option("SNOWFLAKE_HTTP_RETRIES", DEFAULT_HTTP_RETRIES),
query_timeout: env_option("SNOWFLAKE_QUERY_TIMEOUT", DEFAULT_QUERY_TIMEOUT))
private_key = ENV["SNOWFLAKE_PRIVATE_KEY"] || File.read(ENV["SNOWFLAKE_PRIVATE_KEY_PATH"])
private_key =
if key = ENV["SNOWFLAKE_PRIVATE_KEY"]
key
elsif path = ENV["SNOWFLAKE_PRIVATE_KEY_PATH"]
File.read(path)
else
raise MissingConfig.new({}), "Either ENV['SNOWFLAKE_PRIVATE_KEY'] or ENV['SNOWFLAKE_PRIVATE_KEY_PATH'] must be set"
end

new(
ENV["SNOWFLAKE_URI"],
ENV.fetch("SNOWFLAKE_URI"),
private_key,
ENV["SNOWFLAKE_ORGANIZATION"],
ENV["SNOWFLAKE_ACCOUNT"],
ENV["SNOWFLAKE_USER"],
ENV.fetch("SNOWFLAKE_ORGANIZATION"),
ENV.fetch("SNOWFLAKE_ACCOUNT"),
ENV.fetch("SNOWFLAKE_USER"),
ENV["SNOWFLAKE_DEFAULT_WAREHOUSE"],
ENV["SNOWFLAKE_DEFAULT_DATABASE"],
logger: logger,
Expand Down
19 changes: 19 additions & 0 deletions spec/ruby_snowflake/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@
RSpec.describe RubySnowflake::Client do
let(:client) { described_class.from_env }

describe "initialization" do
context "when the environment variables are not set" do
around do |example|
old_env = ENV.to_h

begin
ENV.clear
example.run
ensure
ENV.replace(old_env)
end
end

it "should raise an error" do
expect { client }.to raise_error(RubySnowflake::MissingConfig)
end
end
end

describe "querying" do
let(:query) { "" }
let(:result) { client.query(query) }
Expand Down

0 comments on commit 80ae8f7

Please sign in to comment.