Class: Users::ExceptionHandler

Inherits:
Object
  • Object
show all
Includes:
Common::Client::Concerns::ServiceStatus
Defined in:
app/services/users/exception_handler.rb

Constant Summary

Constants included from Common::Client::Concerns::ServiceStatus

Common::Client::Concerns::ServiceStatus::RESPONSE_STATUS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error, service) ⇒ ExceptionHandler

Returns a new instance of ExceptionHandler.

Parameters:

  • error (ErrorClass)

    An external service error

  • service (String)

    The name of the external service (i.e. ‘VAProfile’ or ‘MVI’)



16
17
18
19
# File 'app/services/users/exception_handler.rb', line 16

def initialize(error, service)
  @error = validate!(error)
  @service = service
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



11
12
13
# File 'app/services/users/exception_handler.rb', line 11

def error
  @error
end

#serviceObject (readonly)

Returns the value of attribute service.



11
12
13
# File 'app/services/users/exception_handler.rb', line 11

def service
  @service
end

Instance Method Details

#base_errorObject (private)



62
63
64
65
66
67
68
69
# File 'app/services/users/exception_handler.rb', line 62

def base_error
  exception = error.errors.first

  error_template.merge(
    description: "#{exception.code}, #{exception.status}, #{exception.title}, #{exception.detail}",
    status: exception.status.to_i
  )
end

#client_errorObject (private)



71
72
73
74
75
76
# File 'app/services/users/exception_handler.rb', line 71

def client_error
  error_template.merge(
    description: "#{error.class}, #{error.status}, #{error.message}, #{error.body}",
    status: error.status.to_i
  )
end


92
93
94
95
96
97
# File 'app/services/users/exception_handler.rb', line 92

def client_error_related_to_title38
  error_template.merge(
    description: "#{error.class}, Client error related to title38",
    status: 404
  )
end

#error_templateObject (private)



113
114
115
116
117
118
119
120
121
# File 'app/services/users/exception_handler.rb', line 113

def error_template
  {
    external_service: service,
    start_time: Time.current.iso8601,
    end_time: nil,
    description: nil,
    status: nil
  }
end

#mpi_error(status) ⇒ Object (private)



99
100
101
102
103
104
# File 'app/services/users/exception_handler.rb', line 99

def mpi_error(status)
  error_template.merge(
    description: "#{error.class}, #{error.message}",
    status:
  )
end

#serialize_errorHash

Serializes the initialized error into one of the predetermined error types. Uses error classes that can be triggered by MVI or VA Profile (formerly Vet360).

The serialized error format is modelled after the Maintenance Windows schema, per the FE’s request.

window schema.

Returns:

  • (Hash)

    A serialized version of the initialized error. Follows maintenance

See Also:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/services/users/exception_handler.rb', line 31

def serialize_error
  case error
  when Common::Exceptions::BaseError
    base_error
  when VAProfile::VeteranStatus::VAProfileError
    if error.status == 404
      title_error(:not_found)
    else
      standard_va_profile_error
    end
  when Common::Client::Errors::ClientError
    client_error
  when MPI::Errors::RecordNotFound
    mpi_error(404)
  when MPI::Errors::FailedRequestError
    mpi_error(503)
  when MPI::Errors::DuplicateRecords
    mpi_error(404)
  else
    standard_error
  end
end

#standard_errorObject (private)



106
107
108
109
110
111
# File 'app/services/users/exception_handler.rb', line 106

def standard_error
  error_template.merge(
    description: "#{error.class}, #{error.message}, #{error}",
    status: standard_error_status(error)
  )
end

#standard_error_status(error) ⇒ Object (private)



123
124
125
126
127
128
# File 'app/services/users/exception_handler.rb', line 123

def standard_error_status(error)
  error.try(:status).presence ||
    error.try(:status_code).presence ||
    error.try(:code).presence ||
    503
end

#standard_va_profile_errorObject (private)



85
86
87
88
89
90
# File 'app/services/users/exception_handler.rb', line 85

def standard_va_profile_error
  error_template.merge(
    description: "#{error.class}, #{error.message}, #{error} VA Profile failure",
    status: standard_error_status(error)
  )
end

#title_error(_type) ⇒ Object (private)



78
79
80
81
82
83
# File 'app/services/users/exception_handler.rb', line 78

def title_error(_type)
  error_template.merge(
    description: "#{error.class}, 404 Veteran Status title not found",
    status: 404
  )
end

#validate!(error) ⇒ Object (private)



56
57
58
59
60
# File 'app/services/users/exception_handler.rb', line 56

def validate!(error)
  raise Common::Exceptions::ParameterMissing.new('error'), 'error' if error.blank?

  error
end