Class: Forms::SubmissionStatuses::ErrorHandler
- Inherits:
-
Object
- Object
- Forms::SubmissionStatuses::ErrorHandler
- Defined in:
- lib/forms/submission_statuses/error_handler.rb
Constant Summary collapse
- SOURCE =
'Lighthouse - Benefits Intake API'
- TITLE_PREFIX =
'Form Submission Status'
Class Method Summary collapse
- .title_from(status) ⇒ Object (also: detail_from)
Instance Method Summary collapse
- #handle_error(status:, body:) ⇒ Object
- #normalize(status:, title:, detail:) ⇒ Object
- #parse_error(status, body) ⇒ Object
Class Method Details
.title_from(status) ⇒ Object Also known as: detail_from
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/forms/submission_statuses/error_handler.rb', line 41 def title_from(status) status_titles = { 401 => 'Unauthorized', 403 => 'Forbidden', 413 => 'Request Entity Too Large', 422 => 'Unprocessable Content', 429 => 'Too Many Requests', 500 => 'Internal Server Error', 502 => 'Bad Gateway', 504 => 'Gateway Timeout' } status_titles.fetch(status, 'Unknown Error') end |
Instance Method Details
#handle_error(status:, body:) ⇒ Object
9 10 11 12 |
# File 'lib/forms/submission_statuses/error_handler.rb', line 9 def handle_error(status:, body:) errors = parse_error(status, body) errors.is_a?(Array) ? errors : [errors] end |
#normalize(status:, title:, detail:) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/forms/submission_statuses/error_handler.rb', line 31 def normalize(status:, title:, detail:) { status:, source: SOURCE, title: "#{TITLE_PREFIX}: #{title}", detail: } end |
#parse_error(status, body) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/forms/submission_statuses/error_handler.rb', line 14 def parse_error(status, body) error_msg = body.transform_keys(&:to_sym) title = self.class.title_from(status) case error_msg in { message: } normalize(status:, title:, detail: ) in { detail: detail } normalize(status:, title:, detail:) in { errors: errors } # recursive call to normalize a collection of errors errors.map { |e| parse_error(status, e) } else normalize(status:, title:, detail: self.class.detail_from(status)) end end |