Sha256: f6f835a771ea359e61e312c5fa05b907048437531673f6cd8b070457d1ab7988
Contents?: true
Size: 1.72 KB
Versions: 1
Compression:
Stored size: 1.72 KB
Contents
# frozen_string_literal: true require 'dry-monads' module ApiResponse class Processor class Failure include Dry::Monads[:result] extend Dry::Initializer param :response, type: Types.Interface(:body) option :config, default: -> { ApiResponse.config } def call return response if config.raw_response return build_error_monad if config.monad begin return response_body if config.error_json rescue StandardError return config.default_return_value || response.body end config.default_return_value end private def response_body @response_body ||= config.parser.new(response, config: config).call end def build_error_monad status = config.default_status || prepare_status(response) error = config.default_error || response_body.fetch(:error, nil) || response_body error_key = config.default_error_key || response_body.fetch(:error_key, nil) Failure({error: error, error_key: error_key, status: status}) end def prepare_status(response) code = case config.adapter when :faraday, :excon then response.status else response&.code end prepared_default_status || prepared_response_status(code) end def prepared_response_status(code) config.symbol_status ? ApiResponse::Types::STATUS_CODE_TO_SYMBOL[code.to_i] : code.to_i end def prepared_default_status if config.symbol_status && config.default_status.is_a?(Integer) ApiResponse::Types::SYMBOL_TO_STATUS_CODE[config.default_status] else config.default_status end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
api-response-presenter-0.0.1 | lib/api_response/processor/failure.rb |