Exception: StreamChat::StreamAPIException

Inherits:
StandardError
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/stream-chat/errors.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ StreamAPIException

Returns a new instance of StreamAPIException.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/stream-chat/errors.rb', line 21

def initialize(response)
  super()
  @response = response

  # Seed defaults first so the typed readers never return nil. A 5xx can
  # arrive with an empty or non-JSON body (e.g. a load balancer emitting a
  # bare 503), and an error envelope may omit "code"/"message" or send a
  # non-integer "code". Without these defaults, reading error_code on such
  # an exception raised a Sorbet TypeError that masked the real HTTP error.
  @json_response = T.let(false, T::Boolean)
  @error_code = T.let(-1, Integer)
  @error_message = T.let('unknown', String)

  begin
    parsed_response = JSON.parse(response.body)
  rescue JSON::ParserError
    return
  end
  return unless parsed_response.is_a?(Hash)

  @json_response = true
  code = parsed_response['code']
  @error_code = code if code.is_a?(Integer)
  msg = parsed_response['message']
  @error_message = msg if msg.is_a?(String)
end

Instance Attribute Details

#error_codeObject (readonly)

Returns the value of attribute error_code.



9
10
11
# File 'lib/stream-chat/errors.rb', line 9

def error_code
  @error_code
end

#error_messageObject (readonly)

Returns the value of attribute error_message.



12
13
14
# File 'lib/stream-chat/errors.rb', line 12

def error_message
  @error_message
end

#json_responseObject (readonly)

Returns the value of attribute json_response.



15
16
17
# File 'lib/stream-chat/errors.rb', line 15

def json_response
  @json_response
end

#responseObject (readonly)

Returns the value of attribute response.



18
19
20
# File 'lib/stream-chat/errors.rb', line 18

def response
  @response
end

Instance Method Details

#messageObject



49
50
51
52
53
54
55
# File 'lib/stream-chat/errors.rb', line 49

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

#to_sObject



58
59
60
# File 'lib/stream-chat/errors.rb', line 58

def to_s
  message
end