class BrregGrunndata::Client::ResponseValidator

Constants

RESPONSE_SUB_STATUS_CODE_TO_ERROR

A map of brreg's sub status codes to error class we will raise

Public Class Methods

new(response) click to toggle source
# File lib/brreg_grunndata/client/response_validator.rb, line 27
def initialize(response)
  @response = response
  @header = response.header
end

Public Instance Methods

raise_error_or_return_response!() click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/brreg_grunndata/client/response_validator.rb, line 33
def raise_error_or_return_response!
  return @response if @header.success?

  error_class = nil
  error_sub_status = nil

  case @header.sub_statuses.length
  when 0
    error_sub_status = 'Not included in response'
    error_class = UnexpectedError
  when 1
    error_sub_status = @header.sub_statuses[0]
    code = error_sub_status[:code]
    error_class = RESPONSE_SUB_STATUS_CODE_TO_ERROR.fetch(code) { UnexpectedError }
  else
    raise Error, "Expected 0 or 1 sub status. Got: #{@header.sub_statuses}"
  end

  raise error_class, error_sub_status
end