From 7f82a4b00fe4f4aa593eccb7bc3912b85d1d7dc1 Mon Sep 17 00:00:00 2001 From: mattwr18 Date: Tue, 12 Sep 2023 17:47:03 +0200 Subject: [PATCH] Inspect error message for challenge token keyword - 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 --captcha --- .../signal_adapter/bad_request_error.rb | 9 +++++ app/adapters/signal_adapter/outbound/text.rb | 35 ++++++++++++++----- docker-compose.yml | 2 +- 3 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 app/adapters/signal_adapter/bad_request_error.rb diff --git a/app/adapters/signal_adapter/bad_request_error.rb b/app/adapters/signal_adapter/bad_request_error.rb new file mode 100644 index 000000000..11c7e7e58 --- /dev/null +++ b/app/adapters/signal_adapter/bad_request_error.rb @@ -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 diff --git a/app/adapters/signal_adapter/outbound/text.rb b/app/adapters/signal_adapter/outbound/text.rb index de3715b90..97875bf94 100644 --- a/app/adapters/signal_adapter/outbound/text.rb +++ b/app/adapters/signal_adapter/outbound/text.rb @@ -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 @@ -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 diff --git a/docker-compose.yml b/docker-compose.yml index 425ec2b56..fdee5fc90 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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)