Sha256: f2cde16f3ae4388d44d3e779aa92945a756ec4ce031d3d53bf0458926b90336b

Contents?: true

Size: 959 Bytes

Versions: 9

Compression:

Stored size: 959 Bytes

Contents

module Brightbox
  class ErrorParser
    include Brightbox::Logging
    attr_accessor :socket_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

9 entries across 9 versions & 1 rubygems

Version Path
brightbox-cli-0.18.1 lib/brightbox-cli/error_parser.rb
brightbox-cli-0.18.0 lib/brightbox-cli/error_parser.rb
brightbox-cli-0.17.5 lib/brightbox-cli/error_parser.rb
brightbox-cli-0.17.4 lib/brightbox-cli/error_parser.rb
brightbox-cli-0.17.3 lib/brightbox-cli/error_parser.rb
brightbox-cli-0.17.2 lib/brightbox-cli/error_parser.rb
brightbox-cli-0.17.1 lib/brightbox-cli/error_parser.rb
brightbox-cli-0.17.0 lib/brightbox-cli/error_parser.rb
brightbox-cli-0.16.0 lib/brightbox-cli/error_parser.rb