Sha256: c29223e5e8a7291264c441855fb4ee3e33f4dd7ec7297428fa66a4772aa905e7
Contents?: true
Size: 1.28 KB
Versions: 4
Compression:
Stored size: 1.28 KB
Contents
# frozen_string_literal: true module FinApps module Middleware class RaiseError < Faraday::Response::Middleware # :nodoc: using ObjectExtensions using StringExtensions SUCCESS_STATUSES = 200..299 CONNECTION_FAILED_STATUS = 407 API_SESSION_TIMEOUT = 419 def on_complete(env) return if SUCCESS_STATUSES.include?(env[:status]) if env[:status] == API_SESSION_TIMEOUT raise(FinApps::Error::ApiSessionTimeoutError, 'Api Session Timed out') elsif env[:status] == CONNECTION_FAILED_STATUS raise(Faraday::Error::ConnectionFailed, '407 "Proxy Authentication Required"') else raise(Faraday::Error::ClientError, response_values(env)) end 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 body.blank? body = body.json_to_hash if body.is_a?(String) has_message_key?(body) ? body['messages'] : nil end def has_message_key?(body) body.respond_to?(:key?) && body.key?('messages') end end end end
Version data entries
4 entries across 4 versions & 1 rubygems