11# frozen_string_literal: true
2- require "faraday"
2+
3+ require 'faraday'
34# @private
45module FaradayMiddleware
56 # @private
@@ -8,10 +9,16 @@ def call(env)
89 @app . call ( env ) . on_complete do |response |
910 case response [ :status ] . to_i
1011 when 400 , 401 , 404 , 500 , 502 , 503 , 504
11- raise ShipEngine ::Exceptions ::ShipEngineError . new ( message : error_body ( response [ :body ] ) , source : error_source ( response [ :body ] ) ,
12- type : error_type ( response [ :body ] ) , code : error_code ( response [ :body ] ) , request_id : response [ :body ] [ "request_id" ] , url : response [ :url ] . to_s )
12+ raise ShipEngine ::Exceptions ::ShipEngineError . new (
13+ message : error_body ( response [ :body ] ) ,
14+ source : error_source ( response [ :body ] ) ,
15+ type : error_type ( response [ :body ] ) ,
16+ code : error_code ( response [ :body ] ) ,
17+ request_id : response [ :body ] [ 'request_id' ] ,
18+ url : response [ :url ] . to_s
19+ )
1320 when 429
14- raise ShipEngine ::Exceptions ::RateLimitError . new ( retries : env . request_headers [ " Retries" ] . to_i , source : error_source ( response [ :body ] ) , request_id : response [ :body ] [ " request_id" ] )
21+ raise ShipEngine ::Exceptions ::RateLimitError . new ( retries : env . request_headers [ ' Retries' ] . to_i , source : error_source ( response [ :body ] ) , request_id : response [ :body ] [ ' request_id' ] )
1522 end
1623 end
1724 end
@@ -23,55 +30,47 @@ def initialize(app)
2330
2431 private
2532
26- def error_message_400 ( response )
33+ def error_message_400 ( response ) # rubocop:todo Naming/VariableNumber
2734 "#{ response [ :method ] . to_s . upcase } #{ response [ :url ] } : #{ response [ :status ] } #{ error_body ( response [ :body ] ) } "
2835 end
2936
3037 def error_body ( body )
31- if !body . nil? && !body . empty? && body . is_a? ( String )
32- body = ::JSON . parse ( body )
33- end
38+ body = ::JSON . parse ( body ) if !body . nil? && !body . empty? && body . is_a? ( String )
3439
3540 if body . nil?
3641 nil
37- elsif body [ " errors" ] && !body [ " errors" ] . empty?
38- body [ " errors" ] [ 0 ] [ " message" ]
42+ elsif body [ ' errors' ] && !body [ ' errors' ] . empty?
43+ body [ ' errors' ] [ 0 ] [ ' message' ]
3944 end
4045 end
4146
4247 def error_source ( body )
43- if !body . nil? && !body . empty? && body . is_a? ( String )
44- body = ::JSON . parse ( body )
45- end
48+ body = ::JSON . parse ( body ) if !body . nil? && !body . empty? && body . is_a? ( String )
4649
4750 if body . nil?
4851 nil
49- elsif body [ " errors" ] && !body [ " errors" ] . empty?
50- body [ " errors" ] [ 0 ] [ " error_source" ]
52+ elsif body [ ' errors' ] && !body [ ' errors' ] . empty?
53+ body [ ' errors' ] [ 0 ] [ ' error_source' ]
5154 end
5255 end
5356
5457 def error_type ( body )
55- if !body . nil? && !body . empty? && body . is_a? ( String )
56- body = ::JSON . parse ( body )
57- end
58+ body = ::JSON . parse ( body ) if !body . nil? && !body . empty? && body . is_a? ( String )
5859
5960 if body . nil?
6061 nil
61- elsif body [ " errors" ] && !body [ " errors" ] . empty?
62- body [ " errors" ] [ 0 ] [ " error_type" ]
62+ elsif body [ ' errors' ] && !body [ ' errors' ] . empty?
63+ body [ ' errors' ] [ 0 ] [ ' error_type' ]
6364 end
6465 end
6566
6667 def error_code ( body )
67- if !body . nil? && !body . empty? && body . is_a? ( String )
68- body = ::JSON . parse ( body )
69- end
68+ body = ::JSON . parse ( body ) if !body . nil? && !body . empty? && body . is_a? ( String )
7069
7170 if body . nil?
7271 nil
73- elsif body [ " errors" ] && !body [ " errors" ] . empty?
74- body [ " errors" ] [ 0 ] [ " error_code" ]
72+ elsif body [ ' errors' ] && !body [ ' errors' ] . empty?
73+ body [ ' errors' ] [ 0 ] [ ' error_code' ]
7574 end
7675 end
7776 end
0 commit comments