@@ -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
105125end
0 commit comments