Exception: Talkgh::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/talkgh/errors/error.rb

Direct Known Subclasses

ClientError, ServerError

Class Method Summary collapse

Class Method Details

.parse(response) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/talkgh/errors/error.rb', line 5

def self.parse(response)
    exception_class = case response
    when Net::HTTPUnauthorized
      AuthenticationError
    when Net::HTTPClientError
      ClientError
    when Net::HTTPServerError
      ServerError
    else
      Error
    end
    
  message = if content_type = response['Content-Type']
    case content_type.split(';').first
    when 'application/problem+json'
      Problem.parse(response.body)
    when 'application/json'
      hash = ::JSON.parse(response.body)
      hash['error_title']
    end
  end
    
  exception_class.new(message)
end