class Gitlab::Error::ResponseError
Custom error class for rescuing from HTTP response errors.
Constants
- POSSIBLE_MESSAGE_KEYS
Public Class Methods
Source
# File lib/gitlab/error.rb, line 18 def initialize(response) @response = response super(build_error_message) end
Calls superclass method
Public Instance Methods
Source
# File lib/gitlab/error.rb, line 40 def error_code if @response.respond_to?(:error_code) @response.error_code else '' end end
Additional error context returned by some API
endpoints
@return [String]
Source
# File lib/gitlab/error.rb, line 33 def response_message @response.parsed_response.message end
Body content returned in the HTTP response
@return [String]
Source
# File lib/gitlab/error.rb, line 26 def response_status @response.code end
Status code returned in the HTTP response.
@return [Integer]
Private Instance Methods
Source
# File lib/gitlab/error.rb, line 53 def build_error_message parsed_response = classified_response message = check_error_keys(parsed_response) "Server responded with code #{@response.code}, message: " \ "#{handle_message(message)}. " \ "Request URI: #{@response.request.base_uri}#{@response.request.path}" end
Human friendly message.
@return [String]
Source
Source
# File lib/gitlab/error.rb, line 71 def classified_response if @response.respond_to?('headers') @response.headers['content-type'] == 'text/plain' ? { message: @response.to_s } : @response.parsed_response else @response.parsed_response end rescue Gitlab::Error::Parsing # Return stringified response when receiving a # parsing error to avoid obfuscation of the # api error. # # note: The Gitlab API does not always return valid # JSON when there are errors. @response.to_s end
Parse the body based on the classification of the body content type
@return parsed response
Source
# File lib/gitlab/error.rb, line 88 def handle_message(message) case message when Gitlab::ObjectifiedHash message.to_h.sort.map do |key, val| "'#{key}' #{(val.is_a?(Hash) ? val.sort.map { |k, v| "(#{k}: #{v.join(' ')})" } : [val].flatten).join(' ')}" end.join(', ') when Array message.join(' ') else message end end
Handle error response message in case of nested hashes