Sha256: 36594065dd41a94586ce0c4cca7a19da4adce83cd30835521620802db852a74e

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module ActionKitRest
  module Response
    class ValidationError < StandardError
      attr_accessor :errors, :url, :body

      def initialize(params)
        self.url = params[:url]
        self.body = params[:body].dup
        self.errors = JSON.parse(params[:body])['errors']
        super()
      end

      def to_s
        "#{super()} \n url: #{url} \n body: #{body.force_encoding('UTF-8')} \n errors: #{errors}"
      rescue Encoding::CompatibilityError
        # Something went gravely wrong trying to construct the error message, so give up on the extra info
        # and just raise the name of the exception.
        # This will let us at least raise with a backtrace.
        super
      end
    end

    class InvalidAkidError < ValidationError
      MATCHING_ERRORS = ['Unable to associate this mailing ID with account.',
                         'לא הצלחנו לקשר בין מספר הזיהוי של רשימת הדיוור הזו לבין החשבון.',
                         'لم ننجح في مطابقة العنوان الالكتروني مع الحساب.'].freeze

      def self.matches?(errors)
        return false unless errors&.keys == ['mailing_id']

        mailing_id_errors = errors['mailing_id']
        return false unless mailing_id_errors.size == 1

        MATCHING_ERRORS.include?(mailing_id_errors.first)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
action_kit_rest-0.4.10 lib/action_kit_rest/response/validation_error.rb
action_kit_rest-0.4.9 lib/action_kit_rest/response/validation_error.rb