Skip to content

Commit

Permalink
Add logging to Cloudflare client
Browse files Browse the repository at this point in the history
  • Loading branch information
tra0x committed Nov 21, 2024
1 parent e924f49 commit cafd10a
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions lib/record_store/provider/cloudflare/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,30 @@ def initialize(api_token)

def get(endpoint, params = {})
uri = build_uri(endpoint, params)

response = request(:get, uri)
Cloudflare::Response.new(response)
end

def post(endpoint, body = nil)
uri = build_uri(endpoint)

response = request(:post, uri, body: body)
Cloudflare::Response.new(response)
end

def put(endpoint, body = nil)
uri = build_uri(endpoint)

response = request(:put, uri, body: body)
Cloudflare::Response.new(response)
end

def patch(endpoint, body = nil)
uri = build_uri(endpoint)

response = request(:patch, uri, body: body)
Cloudflare::Response.new(response)
end

def delete(endpoint)
uri = build_uri(endpoint)

response = request(:delete, uri)
Cloudflare::Response.new(response)
end
Expand Down Expand Up @@ -70,8 +65,19 @@ def request(method, uri, body: nil)
begin
response = conn.request(request)
rescue StandardError => e
$stderr.puts "HTTP error: #{e.message}"
raise "HTTP error: #{e.message}"
end

if response.code.to_i >= 500
$stderr.puts "Server error: #{response.code} #{response.message}"
raise RecordStore::Provider::Error, "Server error: #{response.code} #{response.message}"
elsif response.code.to_i >= 400
error_messages = JSON.parse(response.body)['errors'].map { |error| error['message'] }.join(', ')
$stderr.puts "Client error: #{response.code} #{response.message} - #{error_messages}"
raise RecordStore::Provider::Error, "Client error: #{response.code} #{response.message} - #{error_messages}"
end

response
end
end
Expand Down

0 comments on commit cafd10a

Please sign in to comment.