Skip to content

Commit

Permalink
Improve webhook errors
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaudgg committed Sep 24, 2024
1 parent 3091f65 commit 39e6f82
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class App < Sinatra::Base
member_params = Webhook.handle!(payload)
logger.info payload
logger.info member_params
rescue => e
rescue Webhook::Error => e
logger.info "#{e.class} - #{e.message}"
end

Expand Down
10 changes: 7 additions & 3 deletions lib/webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

class Webhook
attr_reader :payload
class Error < StandardError; end
class UnkownStoreError < Error; end
class IgnoredStatusError < Error; end
class MemberCreationError < Error; end

def self.handle!(payload)
new(payload).handle!
Expand All @@ -28,13 +32,13 @@ def ensure_mapping!
return if mapping

store_name = @payload.dig("store", "name")
raise "Skipped, no mapping found for store: #{store_id} (#{store_name})"
raise UnkownStoreError, "No mapping found for store: #{store_id} (#{store_name})"
end

def ensure_status_completed!
status = @payload["status"]
unless status == "completed"
raise "Skipped, order status is not completed: #{status}"
raise IgnoredStatusError, "Order status is not completed: #{status}"
end
end

Expand All @@ -52,7 +56,7 @@ def submit_member!(params)

response = http.request(request)
unless response.code == "201"
raise "Failed to create member: #{response.code}"
raise MemberCreationError, "Failed to create member: #{response.code}"
end
end

Expand Down

0 comments on commit 39e6f82

Please sign in to comment.