@@ -119,7 +119,7 @@ def initialize(options={})
119
119
faraday . ssl . ca_file = ca_file
120
120
faraday . ssl . verify = true
121
121
faraday . adapter Faraday . default_adapter
122
- end
122
+ end
123
123
return self
124
124
end
125
125
@@ -591,9 +591,12 @@ def execute!(*params)
591
591
592
592
connection = options [ :connection ] || self . connection
593
593
request . authorization = options [ :authorization ] || self . authorization unless options [ :authenticated ] == false
594
+
594
595
tries = 1 + ( options [ :retries ] || self . retries )
596
+
595
597
Retriable . retriable :tries => tries ,
596
- :on => [ TransmissionError ] ,
598
+ :on => [ TransmissionError ] ,
599
+ :on_retry => client_error_handler ( request . authorization ) ,
597
600
:interval => lambda { |attempts | ( 2 ** attempts ) + rand } do
598
601
result = request . send ( connection , true )
599
602
@@ -607,14 +610,6 @@ def execute!(*params)
607
610
} ) )
608
611
raise RedirectError . new ( result . headers [ 'location' ] , result )
609
612
when 400 ...500
610
- if result . status == 401 && request . authorization . respond_to? ( :refresh_token ) && auto_refresh_token
611
- begin
612
- logger . debug ( "Attempting refresh of access token & retry of request" )
613
- request . authorization . fetch_access_token!
614
- rescue Signet ::AuthorizationError
615
- # Ignore since we want the original error
616
- end
617
- end
618
613
raise ClientError . new ( result . error_message || "A client error has occurred" , result )
619
614
when 500 ...600
620
615
raise ServerError . new ( result . error_message || "A server error has occurred" , result )
@@ -665,6 +660,31 @@ def resolve_uri(template, mapping={})
665
660
return Addressable ::Template . new ( @base_uri + template ) . expand ( mapping )
666
661
end
667
662
663
+
664
+ ##
665
+ # Returns on proc for special processing of retries as not all client errors
666
+ # are recoverable. Only 401s should be retried and only if the credentials
667
+ # are refreshable
668
+ #
669
+ # @param [#fetch_access_token!] authorization
670
+ # OAuth 2 credentials
671
+ # @return [Proc]
672
+ def client_error_handler ( authorization )
673
+ can_refresh = authorization . respond_to? ( :refresh_token ) && auto_refresh_token
674
+ Proc . new do |exception , tries |
675
+ next unless exception . kind_of? ( ClientError )
676
+ if exception . result . status == 401 && can_refresh && tries == 1
677
+ begin
678
+ logger . debug ( "Attempting refresh of access token & retry of request" )
679
+ authorization . fetch_access_token!
680
+ next
681
+ rescue Signet ::AuthorizationError
682
+ end
683
+ end
684
+ raise exception
685
+ end
686
+ end
687
+
668
688
end
669
689
670
690
end
0 commit comments