Sha256: 73d8174dc1b4817a5c70529f15e572abafee8501a7a5cb224983c0915ed3c8d5

Contents?: true

Size: 1.3 KB

Versions: 5

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module FaithTeams
  module API
    module V2
      module Error
        # A request failed to complete successfully.
        class Request < StandardError
          attr_reader :response

          # Custom faithteams error about invalid API key
          UNAUTHORIZED_ERROR_MESSAGE = "Unauthorized"

          # Custom faithteams error when a resource is not found
          NOT_FOUND_ERROR_MESSAGE = "Not Found"

          # @param response [HTTP::Response]
          # @param message [String]
          def initialize(response:, message: "Request failed", skip_failure_message: false)
            @response = response
            message += ": #{failure_message}" unless failure_message.blank? || skip_failure_message
            super(message)
          end

          # @return [String]
          def failure_message
            @failure_message ||= response.parse(:json).dig("message") rescue ""
          rescue JSON::ParserError => e
            e.capture
            response.to_s
          end

          # @return [Boolean]
          def unauthorized?
            failure_message == UNAUTHORIZED_ERROR_MESSAGE
          end

          # @return [Boolean]
          def not_found?
            failure_message == NOT_FOUND_ERROR_MESSAGE
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
faithteams-api-4.2.0 lib/faithteams/api/v2/error/request.rb
faithteams-api-4.1.1 lib/faithteams/api/v2/error/request.rb
faithteams-api-4.0.1 lib/faithteams/api/v2/error/request.rb
faithteams-api-2.0.2 lib/faithteams/api/v2/error/request.rb
faithteams-api-2.0.1 lib/faithteams/api/v2/error/request.rb