Skip to content

Commit

Permalink
add: Api.with_ssl_errors_handling
Browse files Browse the repository at this point in the history
  • Loading branch information
iurev committed Sep 19, 2023
1 parent ccc96f9 commit edecea2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
13 changes: 13 additions & 0 deletions src/coverage_reporter/api.cr
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ module CoverageReporter
OpenSSL::SSL::Context::Client.insecure
end

def with_ssl_errors_handling() : HTTP::Client::Response
begin
yield
rescue ex
if OpenSSLVersion.new.can_fail?
Log.error <<-ERROR
Consider upgrading `openssl` library to version >= #{OpenSSLVersion::WORKS} or using --force-insecure-requests flag
ERROR
end
raise(ex)
end
end

def with_redirects(uri : URI, max_redirects : Int32 = 10, & : URI -> HTTP::Client::Response) : HTTP::Client::Response
redirect_num = 0
response = yield(uri)
Expand Down
14 changes: 8 additions & 6 deletions src/coverage_reporter/api/jobs.cr
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ module CoverageReporter
headers.merge!(HTTP::Headers{"Content-Type" => content_type.gsub("\"", "")})

response = Api.with_redirects(jobs_uri) do |uri|
HTTP::Client.post(
uri,
body: body,
headers: headers,
tls: Api.tls_for(uri, @config.force_insecure_requests)
)
Api.with_ssl_errors_handling do
HTTP::Client.post(
uri,
body: body,
headers: headers,
tls: Api.tls_for(uri, @config.force_insecure_requests)
)
end
end

Api.handle_response(response)
Expand Down
14 changes: 8 additions & 6 deletions src/coverage_reporter/api/webhook.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ module CoverageReporter
})

res = Api.with_redirects(webhook_uri) do |uri|
HTTP::Client.post(
uri,
headers: headers,
body: data.to_json,
tls: Api.tls_for(uri, @config.force_insecure_requests)
)
Api.with_ssl_errors_handling do
HTTP::Client.post(
uri,
headers: headers,
body: data.to_json,
tls: Api.tls_for(uri, @config.force_insecure_requests)
)
end
end

Api.handle_response(res)
Expand Down

0 comments on commit edecea2

Please sign in to comment.