diff --git a/Gemfile.lock b/Gemfile.lock index 85b34a3..583aac9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -68,6 +68,7 @@ GEM 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) @@ -76,6 +77,7 @@ GEM 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