Skip to content

Commit e39f96a

Browse files
Added exception method for displaying error messages
1 parent 6cf647f commit e39f96a

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

lib/contentstack/api.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,17 @@ def self.send_request(path, q=nil)
8080
if !@branch.nil? && !@branch.empty?
8181
params["branch"] = @branch
8282
end
83-
ActiveSupport::JSON.decode(URI.open("#{@host}#{@api_version}#{path}#{query}", params).read)
83+
begin
84+
ActiveSupport::JSON.decode(URI.open("#{@host}#{@api_version}#{path}#{query}", params).read)
85+
rescue OpenURI::HTTPError => error
86+
response = error.io
87+
#response.status
88+
# => ["503", "Service Unavailable"]
89+
error_response = JSON.parse(response.string)
90+
error_status = {"status_code" => response.status[0], "status_message" => response.status[1]}
91+
error = error_response.merge(error_status)
92+
raise Contentstack::Error.new(error.to_s)
93+
end
8494
end
8595

8696
def self.send_preview_request(path, q=nil)
@@ -99,7 +109,17 @@ def self.send_preview_request(path, q=nil)
99109
if !@branch.nil? && !@branch.empty?
100110
params["branch"] = @branch
101111
end
102-
ActiveSupport::JSON.decode(URI.open("#{preview_host}#{@api_version}#{path}#{query}",params).read)
112+
begin
113+
ActiveSupport::JSON.decode(URI.open("#{preview_host}#{@api_version}#{path}#{query}",params).read)
114+
rescue OpenURI::HTTPError => error
115+
response = error.io
116+
#response.status
117+
# => ["503", "Service Unavailable"]
118+
error_response = JSON.parse(response.string)
119+
error_status = {"status_code" => response.status[0], "status_message" => response.status[1]}
120+
error = error_response.merge(error_status)
121+
raise Contentstack::Error.new(error.to_s)
122+
end
103123
end
104124
end
105125
end

lib/contentstack/client.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@
33
require 'contentstack/asset_collection'
44
require 'contentstack/sync_result'
55
require 'util'
6+
require 'contentstack/error'
67
module Contentstack
78
class Client
89
using Utility
910
attr_reader :region, :host
1011
# Initialize "Contentstack" Client instance
1112
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?
1219
@region = options[:region].nil? ? Contentstack::Region::US : options[:region]
1320
@host = options[:host].nil? ? get_default_region_hosts(@region) : options[:host]
1421
@live_preview = !options.key?(:live_preview) ? {} : options[:live_preview]

0 commit comments

Comments
 (0)