Sha256: 6e7430618e408e0f22486064a30bd25b3f078b484b91216aaa84f8dd28be3da2

Contents?: true

Size: 821 Bytes

Versions: 1

Compression:

Stored size: 821 Bytes

Contents

module Outreach
  module Errors
    UNAUTHORIZED_ID = 'unauthenticatedRequest'.freeze
    class Unauthorized < StandardError; end

    def check_for_error(_status_code, response_body)
      response_hash = JSON.parse(response_body)
      errors = []
      if !response_hash['errors'].nil? && response_hash['errors'].any?
        errors = response_hash['errors'].map { |error| create_error_hash(error) }
      end

      errors.map do |error|
        case error[:id]
        when UNAUTHORIZED_ID
          raise Unauthorized, error[:detail]
        else
          raise StandardError, error[:title] + ': ' + error[:detail]
        end
      end
    end

    private

    def create_error_hash(error)
      {
        id: error['id'],
        title: error['title'],
        detail: error['detail']
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
outreach-ruby-0.0.2 lib/outreach-ruby/errors.rb