Class: Preservation::Client::ResponseErrorFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/preservation/client/response_error_formatter.rb

Overview

Format HTTP response-related errors

Constant Summary collapse

DEFAULT_BODY =
'Response from preservation-catalog did not contain a body. ' \
'Check honeybadger for preservation-catalog for backtraces, ' \
'and look into adding a `rescue_from` in preservation-catalog ' \
'to provide more details to the client in the future.'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response:, object_id: nil, client_method_name: nil) ⇒ ResponseErrorFormatter

Returns a new instance of ResponseErrorFormatter.



16
17
18
19
20
21
22
23
# File 'lib/preservation/client/response_error_formatter.rb', line 16

def initialize(response:, object_id: nil, client_method_name: nil)
  @req_url = response.env.url
  @status_msg = response.reason_phrase
  @status_code = response.status
  @body = response.body.present? ? response.body : DEFAULT_BODY
  @id = object_id
  @client_method_name = client_method_name
end

Class Method Details

.format(response:, object_id: nil, client_method_name: nil) ⇒ Object



12
13
14
# File 'lib/preservation/client/response_error_formatter.rb', line 12

def self.format(response:, object_id: nil, client_method_name: nil)
  new(response: response, object_id: object_id, client_method_name: client_method_name).format
end

Instance Method Details

#formatObject



25
26
27
28
29
30
# File 'lib/preservation/client/response_error_formatter.rb', line 25

def format
  status_info = status_msg.blank? ? status_code : "#{status_msg} (#{status_code})"
  object_id_info = " for #{id}" if id.present?

  "Preservation::Client.#{client_method_name}#{object_id_info} got #{status_info} from Preservation at #{req_url}: #{body}"
end