Sha256: ab558f90a4924a4436a99fa71aba3304a96f25a69e0377a51b3057889e033dfd

Contents?: true

Size: 1.17 KB

Versions: 8

Compression:

Stored size: 1.17 KB

Contents

# typed: strict
# frozen_string_literal: true

module StreamChat
  class StreamAPIException < StandardError
    extend T::Sig

    sig { returns(Integer) }
    attr_reader :error_code

    sig { returns(String) }
    attr_reader :error_message

    sig { returns(T::Boolean) }
    attr_reader :json_response

    sig { returns(Faraday::Response) }
    attr_reader :response

    sig { params(response: Faraday::Response).void }
    def initialize(response)
      super()
      @response = response
      begin
        parsed_response = JSON.parse(response.body)
        @json_response = T.let(true, T::Boolean)
        @error_code = T.let(parsed_response.fetch('code', 'unknown'), Integer)
        @error_message = T.let(parsed_response.fetch('message', 'unknown'), String)
      rescue JSON::ParserError
        @json_response = false
      end
    end

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

    sig { returns(String) }
    def to_s
      message
    end
  end

  class StreamChannelException < StandardError; end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
stream-chat-ruby-3.7.0 lib/stream-chat/errors.rb
stream-chat-ruby-3.5.1 lib/stream-chat/errors.rb
stream-chat-ruby-3.5.0 lib/stream-chat/errors.rb
stream-chat-ruby-3.4.0 lib/stream-chat/errors.rb
stream-chat-ruby-3.3.0 lib/stream-chat/errors.rb
stream-chat-ruby-3.2.0 lib/stream-chat/errors.rb
stream-chat-ruby-3.1.0 lib/stream-chat/errors.rb
stream-chat-ruby-3.0.0 lib/stream-chat/errors.rb