Method: Morpheus::Cli::ErrorHandler#print_rest_exception

Defined in:
lib/morpheus/cli/error_handler.rb


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/morpheus/cli/error_handler.rb', line 151

def print_rest_exception(err, options)
  e = err
  # heh
  if Morpheus::Logging.debug? && options[:debug].nil?
    options[:debug] = true
  end
  if err.response
    if options[:debug]
      begin
        print_rest_exception_request_and_response(e)
      ensure
        @stderr.print reset
      end
      return
    end
    if err.response.code == 400
      begin
        print_rest_errors(JSON.parse(err.response.to_s), options)
      rescue TypeError, JSON::ParserError => ex
      end
    elsif err.response.code == 404
      begin
        print_rest_errors(JSON.parse(err.response.to_s), options)
      rescue TypeError, JSON::ParserError => ex
        # not json, just 404
        @stderr.print red, "Error Communicating with the remote appliance. #{e}", reset, "\n"  
      end
    else
      @stderr.print red, "Error Communicating with the remote appliance. #{e}", reset, "\n"
      if options[:json] || options[:debug]
        begin
          response = JSON.parse(e.response.to_s)
          # @stderr.print red
          @stderr.print JSON.pretty_generate(response)
          @stderr.print reset, "\n"
        rescue TypeError, JSON::ParserError => ex
          @stderr.print red, "Failed to parse JSON response: #{ex}", reset, "\n"
          # @stderr.print red
          @stderr.print response.to_s
          @stderr.print reset, "\n"
        ensure
          @stderr.print reset
        end
      else
        @stderr.puts "Use -V or --debug for more verbose debugging information."
      end
    end
  else
    @stderr.print red, "Error Communicating with the remote appliance. #{e}", reset, "\n"
  end
  # uh, having this print method return exit_code, err to standardize return values of methods that are still calling it, at the end just by chance..
  # return exit_code, err
  return 1, err
end