Skip to content

Commit

Permalink
Inspect error message for challenge token keyword
Browse files Browse the repository at this point in the history
- If this is part of the error message, we want to parse out the
  challenge token and tag the error with this token so that we can set
up a Sentry filter to send this error to a specific channel whose only
responsibility will be to alert a dev they need to manually solve a
captcha a run signal-cli submitRateLimitChallenge --challenge-token
<TOKEN> --captcha <CAPTCHA>
  • Loading branch information
mattwr18 committed Sep 12, 2023
1 parent 92f5039 commit 7f82a4b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
9 changes: 9 additions & 0 deletions app/adapters/signal_adapter/bad_request_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module SignalAdapter
class BadRequestError < StandardError
def initialize(error_code:, message:)
super("Message was not delivered with error code #{error_code} and message #{message}")
end
end
end
35 changes: 27 additions & 8 deletions app/adapters/signal_adapter/outbound/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,7 @@ def perform(recipient:, text:)
response = Net::HTTP.start(url.host, url.port) do |http|
http.request(request)
end
response.value # may raise exception
rescue Net::HTTPClientException => e
ErrorNotifier.report(e, context: {
code: e.response.code,
message: e.response.message,
headers: e.response.to_hash,
body: e.response.body
})
handle_response(response)
end

def data
Expand All @@ -36,6 +29,32 @@ def data
message: text
}
end

def handle_response(response)
case response.code.to_i
when 200
# TODO: Do something on success. For example, mark the message as delivered?
# Or should we use deliver receipts as the source of truth.
Rails.logger.debug 'Great!'
when 400..599
error_message = JSON.parse(response.body)['error']
exception = SignalAdapter::BadRequestError.new(error_code: response.code, message: error_message)
context = {
code: response.code,
message: response.message,
headers: response.to_hash,
body: error_message
}
tags = if error_message.match?(/with challenge token /)
regex = /"(.*?)"/
challenge_token = error_message.split('with challenge token ').last.match(regex).to_s.split(/"/).last
{ challenge_token: challenge_token }
else
{}
end
ErrorNotifier.report(exception, context: context, tags: tags)
end
end
end
end
end
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ services:
- postgres_data:/var/lib/postgresql/data

signal:
image: bbernhard/signal-cli-rest-api:0.67
image: tactilenews/signal-rest-api:full_error
environment:
- USE_NATIVE=0
#- AUTO_RECEIVE_SCHEDULE=0 22 * * * #enable this parameter on demand (see description below)
Expand Down

0 comments on commit 7f82a4b

Please sign in to comment.