From 5094f520be6c0e3b1f1087a7f68e568071da4595 Mon Sep 17 00:00:00 2001 From: reeshika-h Date: Mon, 15 Dec 2025 12:15:36 +0530 Subject: [PATCH] Update dependencies and improve error handling --- Gemfile.lock | 61 +++++++++++++++++++++----------------- lib/contentstack/api.rb | 6 ++-- lib/contentstack/client.rb | 16 +++++----- lib/contentstack/error.rb | 20 +++++++++++++ 4 files changed, 65 insertions(+), 38 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 505e9c9..3d71715 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -8,41 +8,45 @@ PATH GEM remote: https://rubygems.org/ specs: - activesupport (7.1.3.2) + activesupport (8.1.1) base64 bigdecimal - concurrent-ruby (~> 1.0, >= 1.0.2) + concurrent-ruby (~> 1.0, >= 1.3.1) connection_pool (>= 2.2.5) drb i18n (>= 1.6, < 2) + json + logger (>= 1.4.2) minitest (>= 5.1) - mutex_m - tzinfo (~> 2.0) - addressable (2.8.6) - public_suffix (>= 2.0.2, < 6.0) - base64 (0.2.0) - bigdecimal (3.1.8) - concurrent-ruby (1.2.3) - connection_pool (2.4.1) - contentstack_utils (1.2.0) - activesupport (>= 3.2) - nokogiri (~> 1.11) - crack (1.0.0) + securerandom (>= 0.3) + tzinfo (~> 2.0, >= 2.0.5) + uri (>= 0.13.1) + addressable (2.8.8) + public_suffix (>= 2.0.2, < 8.0) + base64 (0.3.0) + bigdecimal (3.3.1) + concurrent-ruby (1.3.6) + connection_pool (3.0.2) + contentstack_utils (1.2.1) + activesupport (>= 7.0) + nokogiri (>= 1.11) + crack (1.0.1) bigdecimal rexml - diff-lcs (1.5.1) - docile (1.4.0) - drb (2.2.1) - hashdiff (1.1.0) - i18n (1.14.5) + diff-lcs (1.6.2) + docile (1.4.1) + drb (2.2.3) + hashdiff (1.2.1) + i18n (1.14.7) concurrent-ruby (~> 1.0) - minitest (5.22.3) - mutex_m (0.2.0) - nokogiri (1.15.6-arm64-darwin) + json (2.18.0) + logger (1.7.0) + minitest (5.27.0) + nokogiri (1.18.10-arm64-darwin) racc (~> 1.4) - public_suffix (5.0.5) - racc (1.7.3) - rexml (3.2.6) + public_suffix (7.0.0) + racc (1.8.1) + rexml (3.4.4) rspec (3.10.0) rspec-core (~> 3.10.0) rspec-expectations (~> 3.10.0) @@ -56,22 +60,25 @@ GEM diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.10.0) rspec-support (3.10.3) + securerandom (0.4.1) simplecov (0.21.2) docile (~> 1.1) simplecov-html (~> 0.11) simplecov_json_formatter (~> 0.1) - simplecov-html (0.12.3) + simplecov-html (0.13.2) simplecov_json_formatter (0.1.4) tzinfo (2.0.6) concurrent-ruby (~> 1.0) + uri (1.1.1) webmock (3.11.3) addressable (>= 2.3.6) crack (>= 0.3.2) hashdiff (>= 0.4.0, < 2.0.0) - yard (0.9.36) + yard (0.9.38) PLATFORMS arm64-darwin-22 + arm64-darwin-24 DEPENDENCIES contentstack! diff --git a/lib/contentstack/api.rb b/lib/contentstack/api.rb index f68fa5e..88f48b3 100644 --- a/lib/contentstack/api.rb +++ b/lib/contentstack/api.rb @@ -82,7 +82,7 @@ def self.fetch_retry(path, query=nil, count=0) sleep(retryDelay_in_seconds.to_i) #sleep method requires time in seconds as parameter response = fetch_retry(path, query, (count + 1)) else - raise Contentstack::Error.new(response) #Retry Limit exceeded + raise Contentstack::Error.new(Contentstack::ErrorMessages.request_failed(response)) #Retry Limit exceeded end else to_render_content(response) @@ -125,7 +125,7 @@ def self.send_request(path, q=nil) error_response = JSON.parse(response.string) error_status = {"status_code" => response.status[0], "status_message" => response.status[1]} error = error_response.merge(error_status) - raise Contentstack::Error.new(error.to_s) + raise Contentstack::Error.new(Contentstack::ErrorMessages.request_error(error)) end end @@ -165,7 +165,7 @@ def self.send_preview_request(path, q=nil) error_response = JSON.parse(response.string) error_status = {"status_code" => response.status[0], "status_message" => response.status[1]} error = error_response.merge(error_status) - raise Contentstack::Error.new(error.to_s) + raise Contentstack::Error.new(Contentstack::ErrorMessages.request_error(error)) end end diff --git a/lib/contentstack/client.rb b/lib/contentstack/client.rb index e66d5de..7d9a0cd 100644 --- a/lib/contentstack/client.rb +++ b/lib/contentstack/client.rb @@ -10,12 +10,12 @@ class Client attr_reader :region, :host # Initialize "Contentstack" Client instance def initialize(api_key, delivery_token, environment, options={}) - raise Contentstack::Error.new("Api Key is not valid") if api_key.class != String - raise Contentstack::Error.new("Api Key Field Should not be Empty") if api_key.empty? - raise Contentstack::Error.new("Delivery Token is not valid") if delivery_token.class != String - raise Contentstack::Error.new("Delivery Token Field Should not be Empty") if delivery_token.empty? - raise Contentstack::Error.new("Envirnoment Field is not valid") if environment.class != String - raise Contentstack::Error.new("Envirnoment Field Should not be Empty") if environment.empty? + raise Contentstack::Error.new(Contentstack::ErrorMessages::API_KEY_INVALID) if api_key.class != String + raise Contentstack::Error.new(Contentstack::ErrorMessages::API_KEY_REQUIRED) if api_key.empty? + raise Contentstack::Error.new(Contentstack::ErrorMessages::DELIVERY_TOKEN_INVALID) if delivery_token.class != String + raise Contentstack::Error.new(Contentstack::ErrorMessages::DELIVERY_TOKEN_REQUIRED) if delivery_token.empty? + raise Contentstack::Error.new(Contentstack::ErrorMessages::ENVIRONMENT_INVALID) if environment.class != String + raise Contentstack::Error.new(Contentstack::ErrorMessages::ENVIRONMENT_REQUIRED) if environment.empty? @region = options[:region].nil? ? Contentstack::Region::US : options[:region] # @host = options[:host].nil? ? get_default_region_hosts(@region) : options[:host] #removed for not supporting custom host with regions @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={}) "retryLimit"=> @retryLimit, "errorRetry" => @errorRetry } - raise Contentstack::Error.new("Proxy URL Should not be Empty") if @proxy_details.present? && @proxy_details[:url].empty? - raise Contentstack::Error.new("Proxy Port Should not be Empty") if @proxy_details.present? && @proxy_details[:port].empty? + raise Contentstack::Error.new(Contentstack::ErrorMessages::PROXY_URL_REQUIRED) if @proxy_details.present? && @proxy_details[:url].empty? + raise Contentstack::Error.new(Contentstack::ErrorMessages::PROXY_PORT_REQUIRED) if @proxy_details.present? && @proxy_details[:port].empty? API.init_api(api_key, delivery_token, environment, @host, @branch, @live_preview, @proxy_details, retry_options) end diff --git a/lib/contentstack/error.rb b/lib/contentstack/error.rb index 8a68926..288542a 100644 --- a/lib/contentstack/error.rb +++ b/lib/contentstack/error.rb @@ -1,4 +1,24 @@ module Contentstack + # Centralized error messages for the SDK + module ErrorMessages + API_KEY_INVALID = "API Key is invalid. Provide a valid API Key and try again." + API_KEY_REQUIRED = "API Key is required. Provide a valid API Key and try again." + DELIVERY_TOKEN_INVALID = "Delivery Token is invalid. Provide a valid Delivery Token and try again." + DELIVERY_TOKEN_REQUIRED = "Delivery Token is required. Provide a valid Delivery Token and try again." + ENVIRONMENT_INVALID = "Environment is invalid. Provide a valid Environment and try again." + ENVIRONMENT_REQUIRED = "Environment is required. Provide a valid Environment and try again." + PROXY_URL_REQUIRED = "Proxy URL is required. Provide a valid Proxy URL and try again." + PROXY_PORT_REQUIRED = "Proxy Port is required. Provide a valid Proxy Port and try again." + + def self.request_failed(response) + "The request could not be completed due to #{response}. Review the details and try again." + end + + def self.request_error(error) + "The request encountered an issue due to #{error}. Review the details and try again." + end + end + class Error < StandardError def initialize(msg="Something Went Wrong.") super