Sha256: ac862b452c14dc1d54af8610ab05a2a09773f316fd07ce28a90c2fffb1ec5065

Contents?: true

Size: 1.03 KB

Versions: 17

Compression:

Stored size: 1.03 KB

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(err)
      if err.respond_to?(:response) && err.response.respond_to?(:body)
        begin
          json_response = MultiJson.load(err.response.body)
          extract_response_from_json(json_response, err)
        rescue StandardError
          {}
        end
      else
        error "ERROR: #{err}"
      end
    end

    def extract_response_from_json(error_json, err)
      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: #{err}"
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
brightbox-cli-5.0.0.rc2 lib/brightbox-cli/error_parser.rb
brightbox-cli-5.0.0.rc1 lib/brightbox-cli/error_parser.rb
brightbox-cli-5.0.0.alpha lib/brightbox-cli/error_parser.rb
brightbox-cli-4.8.0 lib/brightbox-cli/error_parser.rb
brightbox-cli-4.7.0 lib/brightbox-cli/error_parser.rb
brightbox-cli-4.6.0 lib/brightbox-cli/error_parser.rb
brightbox-cli-4.5.0 lib/brightbox-cli/error_parser.rb
brightbox-cli-4.5.0.rc1 lib/brightbox-cli/error_parser.rb
brightbox-cli-4.4.0 lib/brightbox-cli/error_parser.rb
brightbox-cli-4.3.2 lib/brightbox-cli/error_parser.rb
brightbox-cli-4.3.1 lib/brightbox-cli/error_parser.rb
brightbox-cli-4.3.0 lib/brightbox-cli/error_parser.rb
brightbox-cli-4.2.1 lib/brightbox-cli/error_parser.rb
brightbox-cli-4.2.0 lib/brightbox-cli/error_parser.rb
brightbox-cli-4.1.0 lib/brightbox-cli/error_parser.rb
brightbox-cli-4.0.0 lib/brightbox-cli/error_parser.rb
brightbox-cli-4.0.0.rc2 lib/brightbox-cli/error_parser.rb