From c8fd5d86aca60f8f20b98c07b4ea1c9cb01de416 Mon Sep 17 00:00:00 2001 From: Chidi Ekuma Date: Fri, 15 Mar 2024 13:22:26 +0000 Subject: [PATCH] Debug 502 Faraday Error Refactor user_invite.rb to handle API errors --- .../schools/dfe_sign_in_api/user_invite.rb | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/app/services/schools/dfe_sign_in_api/user_invite.rb b/app/services/schools/dfe_sign_in_api/user_invite.rb index 6ca8e23a3..4cd29f79c 100644 --- a/app/services/schools/dfe_sign_in_api/user_invite.rb +++ b/app/services/schools/dfe_sign_in_api/user_invite.rb @@ -25,13 +25,22 @@ def create def response raise ApiDisabled unless enabled? - resp = faraday.post(endpoint) do |req| - req.headers['Authorization'] = "bearer #{token}" - req.headers['Content-Type'] = 'application/json' - req.body = payload.to_json - end + begin + resp = faraday.post(endpoint) do |req| + req.headers['Authorization'] = "bearer #{token}" + req.headers['Content-Type'] = 'application/json' + req.body = payload.to_json + end + + if resp.success? + return resp.body + else + raise "HTTP Error: #{resp.status} - #{resp.reason_phrase}\n#{resp.body}" + end - resp.body + rescue StandardError => e + raise "Request Failed: #{e.message}\n#{e.backtrace.join("\n")}" + end end def service_id