Sha256: 0937e410f306981ad6529ed18a35bdb01b5fc70af33cbea9f70325cd330e07fe

Contents?: true

Size: 1.8 KB

Versions: 2

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

using ObjectExtensions
using StringExtensions

module FinAppsCore
  module Middleware
    class RaiseError < Faraday::Response::Middleware # :nodoc:
      SUCCESS_STATUSES = (200..299).freeze
      API_UNAUTHENTICATED = 401
      FORBIDDEN = 403
      CONNECTION_FAILED_STATUS = 407
      API_SESSION_TIMEOUT = 419
      LOCKOUT_MESSAGE = 'account is locked'

      def on_complete(env)
        return if SUCCESS_STATUSES.include?(env[:status])

        raise(FinAppsCore::ApiUnauthenticatedError, 'API Invalid Session') if env[:status] == API_UNAUTHENTICATED
        raise(FinAppsCore::ApiSessionTimeoutError, 'API Session Timed out') if env[:status] == API_SESSION_TIMEOUT
        raise(FinAppsCore::ConnectionFailedError, 'Connection Failed') if env[:status] == CONNECTION_FAILED_STATUS
        raise(FinAppsCore::UserLockoutError, 'User is Locked') if user_is_locked?(env)

        raise(Faraday::Error::ClientError, response_values(env))
      end

      def response_values(env)
        {
          status: env.status,
          headers: env.response_headers,
          body: env.body,
          error_messages: error_messages(env.body)
        }
      end

      private

      def error_messages(body)
        return nil if empty?(body)

        hash = to_hash body
        messages hash
      end

      def messages(hash)
        return nil unless hash.respond_to?(:key?) && hash.key?('messages')

        hash['messages']
      end

      def to_hash(source)
        return source unless source.is_a?(String)

        source.json_to_hash
      end

      def empty?(obj)
        obj.nil? || (obj.respond_to?(:empty?) && obj.empty?)
      end

      def user_is_locked?(env)
        env.status == FORBIDDEN && error_messages(env.body)&.[](0)&.downcase == LOCKOUT_MESSAGE
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
finapps_core-5.0.0 lib/finapps_core/middleware/response/raise_error.rb
finapps_core-4.0.7 lib/finapps_core/middleware/response/raise_error.rb