Sha256: 768db8bf7ee41082ec93560253552985ae8ff3b456ab3bacb6f56580e875a7b9

Contents?: true

Size: 972 Bytes

Versions: 4

Compression:

Stored size: 972 Bytes

Contents

module Brightbox
  class ErrorParser
    include Brightbox::Logging
    attr_accessor :socket_error, :token_error

    def initialize(socket_error)
      @socket_error = socket_error
    end

    def pretty_print
      case socket_error
      when Excon::Errors::ServiceUnavailable
        error "Api currently unavailable"
      else
        parse_http_error(socket_error)
      end
    end

    def parse_http_error(e)
      if e.respond_to?(:response) and e.response.respond_to?(:body)
        json_response = JSON.parse(e.response.body) rescue {}
        extract_response_from_json(json_response,e)
      else
        error "ERROR: #{e}"
      end
    end

    def extract_response_from_json(error_json,e)
      json_error = error_json['errors'] || error_json['error']
      if json_error && !json_error.empty?
        error_string = Array(json_error).join(" ")
        error "ERROR: #{error_string}"
      else
        error "ERROR: #{e}"
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
brightbox-cli-1.1.0 lib/brightbox-cli/error_parser.rb
brightbox-cli-1.0.0 lib/brightbox-cli/error_parser.rb
brightbox-cli-1.0.0.rc2 lib/brightbox-cli/error_parser.rb
brightbox-cli-1.0.0.rc1 lib/brightbox-cli/error_parser.rb