This repository was archived by the owner on Jul 19, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +48
-2
lines changed Expand file tree Collapse file tree 3 files changed +48
-2
lines changed Original file line number Diff line number Diff line change 1
- require 'active_support/concern'
1
+ require "active_support/concern"
2
+ require "cc/service/response_check"
2
3
3
4
module CC ::Service ::HTTP
4
5
extend ActiveSupport ::Concern
@@ -52,8 +53,8 @@ def http(options = {})
52
53
options [ :ssl ] [ :ca_file ] ||= ca_file
53
54
54
55
Faraday . new ( options ) do |b |
56
+ b . use ( CC ::Service ::ResponseCheck )
55
57
b . request ( :url_encoded )
56
- b . response ( :raise_error )
57
58
b . adapter ( *Array ( options [ :adapter ] || config [ :adapter ] ) )
58
59
end
59
60
end
Original file line number Diff line number Diff line change @@ -22,9 +22,16 @@ def call
22
22
private
23
23
24
24
def error_message ( ex )
25
+ if ex . respond_to? ( :response_body )
26
+ response_body = ". Response: <#{ ex . response_body . inspect } >"
27
+ else
28
+ response_body = ""
29
+ end
30
+
25
31
message = "Exception invoking service:"
26
32
message << " [#{ @prefix } ]" if @prefix
27
33
message << " (#{ ex . class } ) #{ ex . message } "
34
+ message << response_body
28
35
end
29
36
end
30
37
end
Original file line number Diff line number Diff line change
1
+ class CC ::Service
2
+ class HTTPError < StandardError
3
+ attr_reader :response_body
4
+
5
+ def initialize ( message , response_body )
6
+ @response_body = response_body
7
+
8
+ super ( message )
9
+ end
10
+ end
11
+
12
+ class ResponseCheck < Faraday ::Response ::Middleware
13
+ ErrorStatuses = 400 ...600
14
+
15
+ def on_complete ( env )
16
+ if ErrorStatuses === env [ :status ]
17
+ message = error_message ( env ) ||
18
+ "API request unsuccessful (#{ env [ :status ] } )"
19
+
20
+ raise HTTPError . new ( message , env [ :body ] )
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def error_message ( env )
27
+ # We only handle Jira (or responses which look like Jira's). We will add
28
+ # more logic here over time to account for other service's typical error
29
+ # responses as we see them.
30
+ if env [ :response_headers ] [ "content-type" ] =~ /application\/ json/
31
+ errors = JSON . parse ( env [ :body ] ) [ "errors" ]
32
+ errors . is_a? ( Hash ) && errors . values . map ( &:capitalize ) . join ( ", " )
33
+ end
34
+ rescue JSON ::ParserError
35
+ end
36
+
37
+ end
38
+ end
You can’t perform that action at this time.
0 commit comments