Exception: NaranyaEcm::Rest::RestError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/naranya_ecm/rest/errors.rb

Overview

REST Errors

Generic REST exception class.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(given_response, msg = "") ⇒ RestError

Returns a new instance of RestError.



11
12
13
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
# File 'lib/naranya_ecm/rest/errors.rb', line 11

def initialize(given_response, msg = "")
  @response = given_response

  # Generate a default message if we got a blank message:
  if msg.blank?
    response_env = given_response.env
    msg = response_env.method.upcase.to_s
    msg += " '#{response_env.url.to_s}'"
    msg += " failed (HTTP #{response_env.status})"
  end

  # Record custom attributes in NewRelic:
  if ::NContent::SDK.config.new_relic_agent_available?
    ncontent_url = given_response.env.url

    # Parsear el query string a hash:
    ncontent_request_params = CGI.parse ncontent_url.query.to_s

    # Agregar al hash de ncontent_request_params el request_body como hash:
    if given_response.env.request_headers['Content-Type'] == 'application/json' && given_response.env[:request_body].present?
      ncontent_request_params.merge! ActiveSupport::JSON.decode(given_response.env[:request_body])
    end

    ::NewRelic::Agent.add_custom_attributes \
      ncontent_url: ncontent_url.to_s.split('?').first,
      request_params: ncontent_request_params
  end

  super(msg)
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



9
10
11
# File 'lib/naranya_ecm/rest/errors.rb', line 9

def response
  @response
end

Class Method Details

.build_from_failed_response(response, message = "") ⇒ Object

Captura un response y genera una excepción.



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/naranya_ecm/rest/errors.rb', line 44

def self.build_from_failed_response(response, message = "")
  klass = case response.status
  when 401 then AuthorizationRequired
  when 403 then Forbidden
  when 404 then ResourceNotFound
  when 422 then UnprocessableEntity
  else RemoteFailed
  end

  klass.new response
end

.raise_by_failed_response(response, message = "") ⇒ Object

Captura un response y avienta una excepción.



58
59
60
# File 'lib/naranya_ecm/rest/errors.rb', line 58

def self.raise_by_failed_response(response, message = "")
  raise build_from_failed_response(response, message)
end