Exception: Yao::ServerError

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, requested_env) ⇒ ServerError

Returns a new instance of ServerError.



7
8
9
10
11
# File 'lib/yao/error.rb', line 7

def initialize(message, requested_env)
  @status = requested_env.status
  @env = requested_env
  super(message)
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



12
13
14
# File 'lib/yao/error.rb', line 12

def env
  @env
end

#statusObject (readonly)

Returns the value of attribute status.



12
13
14
# File 'lib/yao/error.rb', line 12

def status
  @status
end

Class Method Details

.detect(env) ⇒ Object



14
15
16
17
18
19
20
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
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/yao/error.rb', line 14

def self.detect(env)
  case env.status
  when 400
    if env.body && env.body["computeFault"]
      ComputeFault.new(extract_message(env.body), env)
    elsif env.body && env.body.to_a.join.include?('NetworkNotFound')
      NetworkNotFound.new(extract_message(env.body), env)
    else
      BadRequest.new(extract_message(env.body), env)
    end
  when 401
    Unauthorized.new(extract_message(env.body), env)
  when 404
    if env.body && env.body["itemNotFound"]
      ItemNotFound.new(extract_message(env.body), env)
    else
      NotFound.new("The resource could not be found.", env)
    end
  when 405
    BadMethod.new(extract_message(env.body), env)
  when 409
    if env.body && env.body["buildInProgress"]
      BuildInProgress.new(extract_message(env.body), env)
    else
      Conflict.new(extract_message(env.body), env)
    end
  when 413
    OverLimit.new(extract_message(env.body), env)
  when 415
    BadMediaType.new(extract_message(env.body), env)
  when 422
    UnprocessableEntity.new(extract_message(env.body), env)
  when 500
    ComputeFault.new(extract_message(env.body), env)
  when 503
    if env.body && env.body[" serverCapacityUnavailable"]
      ServerCapacityUnavailable.new(extract_message(env.body), env)
    else
      ServiceUnavailable.new(extract_message(env.body), env)
    end
  else
    new(extract_message(env.body), env)
  end
rescue => e
  new("Detection failed - %s" % e.message, env)
end

.extract_message(body) ⇒ Object



61
62
63
64
65
# File 'lib/yao/error.rb', line 61

def self.extract_message(body)
  body.values.first["message"] or raise
rescue
  "Something is wrong. - %s" % body.inspect
end