Sha256: 5f9995bd28eae44e273cb4430a8c7bdc78c688496e1c197e77104d799967c272

Contents?: true

Size: 891 Bytes

Versions: 12

Compression:

Stored size: 891 Bytes

Contents

# frozen_string_literal: true

# lib/errors.rb

module StreamChat
  class StreamAPIException < StandardError
    attr_reader :error_code
    attr_reader :error_message

    def initialize(response)
      super()
      @response = response
      begin
        parsed_response = JSON.parse(response.body)
        @json_response = true
        @error_code = parsed_response.fetch('code', 'unknown')
        @error_message = parsed_response.fetch('message', 'unknown')
      rescue JSON::ParserError
        @json_response = false
      end
    end

    def message
      if @json_response
        "StreamChat error code #{@error_code}: #{@error_message}"
      else
        "StreamChat error HTTP code: #{@response.status}"
      end
    end

    def json_response?
      @json_response
    end

    def to_s
      message
    end
  end

  class StreamChannelException < StandardError; end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
stream-chat-ruby-2.20.0 lib/stream-chat/errors.rb
stream-chat-ruby-2.19.0 lib/stream-chat/errors.rb
stream-chat-ruby-2.18.0 lib/stream-chat/errors.rb
stream-chat-ruby-2.17.2 lib/stream-chat/errors.rb
stream-chat-ruby-2.17.1 lib/stream-chat/errors.rb
stream-chat-ruby-2.17.0 lib/stream-chat/errors.rb
stream-chat-ruby-2.16.0 lib/stream-chat/errors.rb
stream-chat-ruby-2.15.0 lib/stream-chat/errors.rb
stream-chat-ruby-2.14.0 lib/stream-chat/errors.rb
stream-chat-ruby-2.13.0 lib/stream-chat/errors.rb
stream-chat-ruby-2.12.0 lib/stream-chat/errors.rb
stream-chat-ruby-2.11.3 lib/stream-chat/errors.rb