Class: VAProfile::Service

Inherits:
Common::Client::Base show all
Defined in:
lib/va_profile/service.rb

Constant Summary collapse

STATSD_KEY_PREFIX =
VAProfile::Stats::STATSD_KEY_PREFIX

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Common::Client::Base

#config, configuration, #connection, #delete, #get, #post, #put, #raise_not_authenticated, #request, #sanitize_headers!, #service_name

Methods included from SentryLogging

#log_exception_to_sentry, #log_message_to_sentry, #non_nil_hash?, #normalize_level, #rails_logger, #set_sentry_metadata

Constructor Details

#initialize(user) ⇒ Service

Returns a new instance of Service.



15
16
17
# File 'lib/va_profile/service.rb', line 15

def initialize(user)
  @user = user
end

Class Method Details

.breakers_serviceObject



27
28
29
# File 'lib/va_profile/service.rb', line 27

def self.breakers_service
  Common::Client::Base.breakers_service
end

Instance Method Details

#final_failure?(error) ⇒ Boolean (private)

Returns:

  • (Boolean)


84
85
86
# File 'lib/va_profile/service.rb', line 84

def final_failure?(error)
  %w[COMPLETED_FAILURE REJECTED].include? error.body&.dig('status')
end

#handle_error(error) ⇒ Object (private)



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/va_profile/service.rb', line 43

def handle_error(error)
  case error
  when Common::Client::Errors::ParsingError # VAProfile sent a non-JSON response
    Sentry.set_extras(
      message: error.message,
      url: config.base_path
    )
    raise_backend_exception('VET360_502', self.class)
  when Common::Client::Errors::ClientError
    save_error_details(error)
    raise Common::Exceptions::Forbidden if error.status == 403

    raise_invalid_body(error, self.class) unless error.body.is_a?(Hash)
    code = parse_messages(error)&.first.try(:[], 'code')
    raise_backend_exception("VET360_#{code}", self.class, error) if code.present?
    raise_backend_exception('VET360_502', self.class, error)
  else
    raise error
  end
end

#log_dates(body) ⇒ Object (private)



33
34
35
36
37
38
39
40
41
# File 'lib/va_profile/service.rb', line 33

def log_dates(body)
  parsed_body = JSON.parse(body)

  Sentry.set_extras(
    request_dates: parsed_body['bio'].slice('effectiveStartDate', 'effectiveEndDate', 'sourceDate')
  )
rescue
  nil
end

#parse_messages(error) ⇒ Object (private)



88
89
90
91
# File 'lib/va_profile/service.rb', line 88

def parse_messages(error)
  messages = error.body&.dig('messages')
  messages&.map { |m| VAProfile::Models::Message.build_from(m) }
end

#perform(method, path, body = nil, headers = {}) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/va_profile/service.rb', line 19

def perform(method, path, body = nil, headers = {})
  log_dates(body)

  VAProfile::Stats.increment('total_operations')
  config.base_request_headers.merge(headers)
  super(method, path, body, headers)
end

#person_transaction?(error) ⇒ Boolean (private)

Returns:

  • (Boolean)


80
81
82
# File 'lib/va_profile/service.rb', line 80

def person_transaction?(error)
  error&.backtrace&.join(',')&.include? 'get_person_transaction_status'
end

#person_transaction_failure?(error) ⇒ Boolean (private)

Returns:

  • (Boolean)


76
77
78
# File 'lib/va_profile/service.rb', line 76

def person_transaction_failure?(error)
  person_transaction?(error) && final_failure?(error)
end

#raise_backend_exception(key, source, error = nil) ⇒ Object (private)



93
94
95
96
# File 'lib/va_profile/service.rb', line 93

def raise_backend_exception(key, source, error = nil)
  report_stats_on(key)
  super
end

#raise_invalid_body(error, source) ⇒ Object (private)



98
99
100
101
102
103
104
105
106
107
# File 'lib/va_profile/service.rb', line 98

def raise_invalid_body(error, source)
  VAProfile::Stats.increment_exception('VET360_502')

  raise Common::Exceptions::BackendServiceException.new(
    'VET360_502',
    { source: source.to_s },
    502,
    error&.body
  )
end

#report_stats_on(exception_key) ⇒ Object (private)



109
110
111
112
113
114
115
# File 'lib/va_profile/service.rb', line 109

def report_stats_on(exception_key)
  if VAProfile::Exceptions::Parser.instance.known?(exception_key)
    VAProfile::Stats.increment_exception(exception_key)
  else
    log_message_to_sentry('New VAProfile Exceptions Key', :info, key: exception_key)
  end
end

#save_error_details(error) ⇒ Object (private)



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/va_profile/service.rb', line 64

def save_error_details(error)
  Sentry.set_extras(
    message: error.message,
    url: config.base_path,
    body: error.body
  )

  Sentry.set_tags(
    va_profile: person_transaction_failure?(error) ? 'failed_vet360_id_initializations' : 'general_client_error'
  )
end