Exception: Putio::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/putio-ruby/error.rb

Direct Known Subclasses

BadRequest, ServerError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env = nil) ⇒ Error

Returns a new instance of Error.



14
15
16
17
# File 'lib/putio-ruby/error.rb', line 14

def initialize(env=nil)
  @env = env
  super(build_error_message)
end

Class Method Details

.from_env(env) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'lib/putio-ruby/error.rb', line 3

def self.from_env(env)
  status = env.status

  if klass = case status
             when 400..499  then Putio::BadRequest
             when 500..599  then Putio::ServerError
             end
    klass.new(env)
  end
end

Instance Method Details

#build_error_messageObject



29
30
31
32
33
34
35
36
# File 'lib/putio-ruby/error.rb', line 29

def build_error_message
  return nil if @env.nil?

  message =  "#{@env.method.to_s.upcase} "
  message << "#{@env.status} - #{@env.url}\n"
  message << "#{response_error}" unless response_error.nil?
  message
end

#error_messageObject



54
55
56
# File 'lib/putio-ruby/error.rb', line 54

def error_message
  @env.body["error_message"]
end

#error_typeObject



50
51
52
# File 'lib/putio-ruby/error.rb', line 50

def error_type
  @env.body["error_type"]
end

#response_bodyObject



46
47
48
# File 'lib/putio-ruby/error.rb', line 46

def response_body
  @env.body
end

#response_errorObject



19
20
21
22
23
24
25
26
27
# File 'lib/putio-ruby/error.rb', line 19

def response_error
  if response_body && error_type && error_message
    "#{error_type}: #{error_message}"
  elsif response_body && error_message
    error_message
  else
    response_body
  end
end

#response_headersObject



38
39
40
# File 'lib/putio-ruby/error.rb', line 38

def response_headers
  @env.response_headers
end

#response_statusObject



42
43
44
# File 'lib/putio-ruby/error.rb', line 42

def response_status
  @env.status
end