Exception: GSGraph::Exception

Inherits:
StandardError
  • Object
show all
Defined in:
lib/gs_graph/exception.rb

Constant Summary collapse

ERROR_HEADER_MATCHERS =
{
  /invalid_token/ => "InvalidToken",
  /invalid_request/ => "InvalidRequest"
}
ERROR_EXCEPTION_MATCHERS =
{
  /Could\snot\ssave\screative/          => "CreativeNotSaved",
  /QueryLockTimeoutException/           => "QueryLockTimeout",
  /Could\snot\screate\stargeting\sspec/ => "TargetingSpecNotSaved",
  /Could\snot\sfetch\sadgroups/         => "AdgroupFetchFailure",
  /Failed\sto\sopen\sprocess/           => "OpenProcessFailure",
  /Could\snot\scommit\stransaction/     => "TransactionCommitFailure",
  /QueryErrorException/                 => "QueryError",
  /QueryConnectionException/            => "QueryConnection",
  /QueryDuplicateKeyException/          => "QueryDuplicateKey"
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, message, body = '') ⇒ Exception

Returns a new instance of Exception.



81
82
83
84
85
86
87
88
89
# File 'lib/gs_graph/exception.rb', line 81

def initialize(code, message, body = '')
  @code = code
  if body.present?
    response = MultiJson.load(body).with_indifferent_access
    message = response[:error][:message]
    @type = response[:error][:type]
  end
  super message
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



3
4
5
# File 'lib/gs_graph/exception.rb', line 3

def code
  @code
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/gs_graph/exception.rb', line 3

def type
  @type
end

Class Method Details

.handle_httpclient_error(response, headers) ⇒ Object



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
60
61
62
63
64
65
66
67
# File 'lib/gs_graph/exception.rb', line 22

def self.handle_httpclient_error(response, headers)
  return nil unless (response[:error] || response[:error_code] || response[:error_msg])

  # Sometimes we see an error that does not have the "error" field, but instead has both "error_code" and "error_msg"
  # example: {"error_code":1,"error_msg":"An unknown error occurred"}
  if (response[:error_code] && response[:error_msg] && !response[:error])
    raise InternalServerError.new("#{response[:error_code]} :: #{response[:error_msg]}")
  end

  # Check the WWW-Authenticate header, since it seems to be more standardized than the response
  # body error information.
  # The complex lookup is needed to follow the HTTP spec - headers should be looked up
  # without regard to case.
  www_authenticate = headers.select do |name, value|
    name.upcase == "WWW-Authenticate".upcase
  end.to_a.flatten.second
  if www_authenticate
    # Session expiration/invalidation is very common. Check for that first.
    if www_authenticate =~ /invalid_token/ && response[:error][:message] =~ /session has been invalidated/
      raise InvalidSession.new("#{response[:error][:type]} :: #{response[:error][:message]}")
    end

    ERROR_HEADER_MATCHERS.keys.each do |matcher|
      if matcher =~ www_authenticate
        exception_class = GSGraph::const_get(ERROR_HEADER_MATCHERS[matcher])
        raise exception_class.new("#{response[:error][:type]} :: #{response[:error][:message]}")
      end
    end
  end

  # If we can't match on WWW-Authenticate, use the type
  case response[:error][:type]
  when /OAuth/
    raise Unauthorized.new("#{response[:error][:type]} :: #{response[:error][:message]}")
  else
    exception_class = nil
    ERROR_EXCEPTION_MATCHERS.keys.each do |matcher|
      exception_class = GSGraph::const_get(ERROR_EXCEPTION_MATCHERS[matcher]) if matcher =~ response[:error][:message]
    end
    if exception_class
      raise exception_class.new("#{response[:error][:type]} :: #{response[:error][:message]}")
    else
      raise BadRequest.new("#{response[:error][:type]} :: #{response[:error][:message]}")
    end
  end
end

.handle_rack_oauth2_error(e) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/gs_graph/exception.rb', line 69

def self.handle_rack_oauth2_error(e)
  exception = case e.status
  when 400
    BadRequest.new(e.message)
  when 401
    Unauthorized.new(e.message)
  else
    Exception.new(e.status, e.message)
  end
  raise exception
end