Skip to content

Commit 5094f52

Browse files
committed
Update dependencies and improve error handling
1 parent 9bf0a2c commit 5094f52

File tree

4 files changed

+65
-38
lines changed

4 files changed

+65
-38
lines changed

Gemfile.lock

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,45 @@ PATH
88
GEM
99
remote: https://rubygems.org/
1010
specs:
11-
activesupport (7.1.3.2)
11+
activesupport (8.1.1)
1212
base64
1313
bigdecimal
14-
concurrent-ruby (~> 1.0, >= 1.0.2)
14+
concurrent-ruby (~> 1.0, >= 1.3.1)
1515
connection_pool (>= 2.2.5)
1616
drb
1717
i18n (>= 1.6, < 2)
18+
json
19+
logger (>= 1.4.2)
1820
minitest (>= 5.1)
19-
mutex_m
20-
tzinfo (~> 2.0)
21-
addressable (2.8.6)
22-
public_suffix (>= 2.0.2, < 6.0)
23-
base64 (0.2.0)
24-
bigdecimal (3.1.8)
25-
concurrent-ruby (1.2.3)
26-
connection_pool (2.4.1)
27-
contentstack_utils (1.2.0)
28-
activesupport (>= 3.2)
29-
nokogiri (~> 1.11)
30-
crack (1.0.0)
21+
securerandom (>= 0.3)
22+
tzinfo (~> 2.0, >= 2.0.5)
23+
uri (>= 0.13.1)
24+
addressable (2.8.8)
25+
public_suffix (>= 2.0.2, < 8.0)
26+
base64 (0.3.0)
27+
bigdecimal (3.3.1)
28+
concurrent-ruby (1.3.6)
29+
connection_pool (3.0.2)
30+
contentstack_utils (1.2.1)
31+
activesupport (>= 7.0)
32+
nokogiri (>= 1.11)
33+
crack (1.0.1)
3134
bigdecimal
3235
rexml
33-
diff-lcs (1.5.1)
34-
docile (1.4.0)
35-
drb (2.2.1)
36-
hashdiff (1.1.0)
37-
i18n (1.14.5)
36+
diff-lcs (1.6.2)
37+
docile (1.4.1)
38+
drb (2.2.3)
39+
hashdiff (1.2.1)
40+
i18n (1.14.7)
3841
concurrent-ruby (~> 1.0)
39-
minitest (5.22.3)
40-
mutex_m (0.2.0)
41-
nokogiri (1.15.6-arm64-darwin)
42+
json (2.18.0)
43+
logger (1.7.0)
44+
minitest (5.27.0)
45+
nokogiri (1.18.10-arm64-darwin)
4246
racc (~> 1.4)
43-
public_suffix (5.0.5)
44-
racc (1.7.3)
45-
rexml (3.2.6)
47+
public_suffix (7.0.0)
48+
racc (1.8.1)
49+
rexml (3.4.4)
4650
rspec (3.10.0)
4751
rspec-core (~> 3.10.0)
4852
rspec-expectations (~> 3.10.0)
@@ -56,22 +60,25 @@ GEM
5660
diff-lcs (>= 1.2.0, < 2.0)
5761
rspec-support (~> 3.10.0)
5862
rspec-support (3.10.3)
63+
securerandom (0.4.1)
5964
simplecov (0.21.2)
6065
docile (~> 1.1)
6166
simplecov-html (~> 0.11)
6267
simplecov_json_formatter (~> 0.1)
63-
simplecov-html (0.12.3)
68+
simplecov-html (0.13.2)
6469
simplecov_json_formatter (0.1.4)
6570
tzinfo (2.0.6)
6671
concurrent-ruby (~> 1.0)
72+
uri (1.1.1)
6773
webmock (3.11.3)
6874
addressable (>= 2.3.6)
6975
crack (>= 0.3.2)
7076
hashdiff (>= 0.4.0, < 2.0.0)
71-
yard (0.9.36)
77+
yard (0.9.38)
7278

7379
PLATFORMS
7480
arm64-darwin-22
81+
arm64-darwin-24
7582

7683
DEPENDENCIES
7784
contentstack!

lib/contentstack/api.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def self.fetch_retry(path, query=nil, count=0)
8282
sleep(retryDelay_in_seconds.to_i) #sleep method requires time in seconds as parameter
8383
response = fetch_retry(path, query, (count + 1))
8484
else
85-
raise Contentstack::Error.new(response) #Retry Limit exceeded
85+
raise Contentstack::Error.new(Contentstack::ErrorMessages.request_failed(response)) #Retry Limit exceeded
8686
end
8787
else
8888
to_render_content(response)
@@ -125,7 +125,7 @@ def self.send_request(path, q=nil)
125125
error_response = JSON.parse(response.string)
126126
error_status = {"status_code" => response.status[0], "status_message" => response.status[1]}
127127
error = error_response.merge(error_status)
128-
raise Contentstack::Error.new(error.to_s)
128+
raise Contentstack::Error.new(Contentstack::ErrorMessages.request_error(error))
129129
end
130130
end
131131

@@ -165,7 +165,7 @@ def self.send_preview_request(path, q=nil)
165165
error_response = JSON.parse(response.string)
166166
error_status = {"status_code" => response.status[0], "status_message" => response.status[1]}
167167
error = error_response.merge(error_status)
168-
raise Contentstack::Error.new(error.to_s)
168+
raise Contentstack::Error.new(Contentstack::ErrorMessages.request_error(error))
169169
end
170170
end
171171

lib/contentstack/client.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ class Client
1010
attr_reader :region, :host
1111
# Initialize "Contentstack" Client instance
1212
def initialize(api_key, delivery_token, environment, options={})
13-
raise Contentstack::Error.new("Api Key is not valid") if api_key.class != String
14-
raise Contentstack::Error.new("Api Key Field Should not be Empty") if api_key.empty?
15-
raise Contentstack::Error.new("Delivery Token is not valid") if delivery_token.class != String
16-
raise Contentstack::Error.new("Delivery Token Field Should not be Empty") if delivery_token.empty?
17-
raise Contentstack::Error.new("Envirnoment Field is not valid") if environment.class != String
18-
raise Contentstack::Error.new("Envirnoment Field Should not be Empty") if environment.empty?
13+
raise Contentstack::Error.new(Contentstack::ErrorMessages::API_KEY_INVALID) if api_key.class != String
14+
raise Contentstack::Error.new(Contentstack::ErrorMessages::API_KEY_REQUIRED) if api_key.empty?
15+
raise Contentstack::Error.new(Contentstack::ErrorMessages::DELIVERY_TOKEN_INVALID) if delivery_token.class != String
16+
raise Contentstack::Error.new(Contentstack::ErrorMessages::DELIVERY_TOKEN_REQUIRED) if delivery_token.empty?
17+
raise Contentstack::Error.new(Contentstack::ErrorMessages::ENVIRONMENT_INVALID) if environment.class != String
18+
raise Contentstack::Error.new(Contentstack::ErrorMessages::ENVIRONMENT_REQUIRED) if environment.empty?
1919
@region = options[:region].nil? ? Contentstack::Region::US : options[:region]
2020
# @host = options[:host].nil? ? get_default_region_hosts(@region) : options[:host] #removed for not supporting custom host with regions
2121
@host = get_host_by_region(@region, options) # Added new method for custom host support with different regions
@@ -32,8 +32,8 @@ def initialize(api_key, delivery_token, environment, options={})
3232
"retryLimit"=> @retryLimit,
3333
"errorRetry" => @errorRetry
3434
}
35-
raise Contentstack::Error.new("Proxy URL Should not be Empty") if @proxy_details.present? && @proxy_details[:url].empty?
36-
raise Contentstack::Error.new("Proxy Port Should not be Empty") if @proxy_details.present? && @proxy_details[:port].empty?
35+
raise Contentstack::Error.new(Contentstack::ErrorMessages::PROXY_URL_REQUIRED) if @proxy_details.present? && @proxy_details[:url].empty?
36+
raise Contentstack::Error.new(Contentstack::ErrorMessages::PROXY_PORT_REQUIRED) if @proxy_details.present? && @proxy_details[:port].empty?
3737
API.init_api(api_key, delivery_token, environment, @host, @branch, @live_preview, @proxy_details, retry_options)
3838
end
3939

lib/contentstack/error.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
module Contentstack
2+
# Centralized error messages for the SDK
3+
module ErrorMessages
4+
API_KEY_INVALID = "API Key is invalid. Provide a valid API Key and try again."
5+
API_KEY_REQUIRED = "API Key is required. Provide a valid API Key and try again."
6+
DELIVERY_TOKEN_INVALID = "Delivery Token is invalid. Provide a valid Delivery Token and try again."
7+
DELIVERY_TOKEN_REQUIRED = "Delivery Token is required. Provide a valid Delivery Token and try again."
8+
ENVIRONMENT_INVALID = "Environment is invalid. Provide a valid Environment and try again."
9+
ENVIRONMENT_REQUIRED = "Environment is required. Provide a valid Environment and try again."
10+
PROXY_URL_REQUIRED = "Proxy URL is required. Provide a valid Proxy URL and try again."
11+
PROXY_PORT_REQUIRED = "Proxy Port is required. Provide a valid Proxy Port and try again."
12+
13+
def self.request_failed(response)
14+
"The request could not be completed due to #{response}. Review the details and try again."
15+
end
16+
17+
def self.request_error(error)
18+
"The request encountered an issue due to #{error}. Review the details and try again."
19+
end
20+
end
21+
222
class Error < StandardError
323
def initialize(msg="Something Went Wrong.")
424
super

0 commit comments

Comments
 (0)