A platform for community discussion. Free, open, simple.
FIX: correctly display http errors in http request (#42033)
Joffrey JAFFEUX86282c506523
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/executor/http_client.rb b/plugins/discourse-workflows/lib/discourse_workflows/executor/http_client.rbindex 290a2022b7e..74de26bf58a 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/executor/http_client.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/executor/http_client.rb@@ -11,6 +11,7 @@ module DiscourseWorkflowsTIMEOUT_SECONDS = 30DEFAULT_MAX_RETRIES = 0DEFAULT_RETRY_STATUSES = Set[429, 500, 502, 503, 504].freeze+ ERROR_BODY_MAX_BYTES = 10.kilobytesdef initialize(exec_ctx, item_index = 0)@exec_ctx = exec_ctx@@ -31,6 +32,7 @@ module DiscourseWorkflowsfiltered_url = filtered_url_for_logging(config["url"], config["query_params"])raise_node_error!("HTTP #{config["method"]} #{filtered_url} failed with status #{response.status}",+ description: error_body_description(response),)end@@ -50,6 +52,14 @@ module DiscourseWorkflowsprivate+ def error_body_description(response)+ body = response.body.to_s.scrub("").strip+ return if body.empty?+ return body if body.bytesize <= ERROR_BODY_MAX_BYTES++ "#{body.byteslice(0, ERROR_BODY_MAX_BYTES).scrub("")}…"+ end+def build_config(method, url, headers, body, options)config =options.to_h.stringify_keys.merge(diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/node_execution_context_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/node_execution_context_spec.rbindex e7e90d25360..4bb803a9914 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/node_execution_context_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/node_execution_context_spec.rb@@ -933,7 +933,7 @@ RSpec.describe DiscourseWorkflows::Executor::NodeExecutionContext doctx.http_request(method: "GET", url: "https://api.example.com/retry")}.to raise_error(DiscourseWorkflows::NodeError,- "HTTP GET https://api.example.com/retry failed with status 503",+ "HTTP GET https://api.example.com/retry failed with status 503: unavailable",)expect(WebMock).to have_requested(:get, "https://api.example.com/retry").onceend@@ -950,7 +950,7 @@ RSpec.describe DiscourseWorkflows::Executor::NodeExecutionContext doctx.http_request(method: "POST", url: "https://api.example.com/retry")}.to raise_error(DiscourseWorkflows::NodeError,- "HTTP POST https://api.example.com/retry failed with status 503",+ "HTTP POST https://api.example.com/retry failed with status 503: unavailable",)expect(WebMock).to have_requested(:post, "https://api.example.com/retry").onceenddiff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/http_request_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/http_request_spec.rbindex 7c197a00b80..bce1a517af5 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/http_request_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/http_request_spec.rb@@ -179,7 +179,53 @@ RSpec.describe DiscourseWorkflows::Nodes::HttpRequest::V1 doexpect { execute_node(configuration: config, item: item) }.to raise_error(DiscourseWorkflows::NodeError,- "HTTP GET https://api.example.com/fail?token=[FILTERED] failed with status 404",+ "HTTP GET https://api.example.com/fail?token=[FILTERED] failed with status 404: Not Found",+ )+ end++ it "includes the response body in the error message" do+ stub_request(:post, "https://api.example.com/categories").to_return(+ status: 422,+ body: { errors: ["Group PrivateGroup has no access to the parent category"] }.to_json,+ headers: {+ "content-type" => "application/json",+ },+ )++ config = {+ "method" => "POST",+ "url" => "https://api.example.com/categories",+ "body_json" => "{}",+ }++ expect { execute_node(configuration: config, item: item) }.to raise_error(+ DiscourseWorkflows::NodeError,+ /failed with status 422: \{"errors":\["Group PrivateGroup has no access/,+ )+ end++ it "truncates long response bodies in the error message" do+ body = "x" * (DiscourseWorkflows::Executor::HttpClient::ERROR_BODY_MAX_BYTES * 2)+ stub_request(:get, "https://api.example.com/fail").to_return(status: 500, body:)++ config = { "method" => "GET", "url" => "https://api.example.com/fail" }++ expect { execute_node(configuration: config, item: item) }.to raise_error(+ DiscourseWorkflows::NodeError,+ ) do |error|+ expect(error.message).to end_with("…")+ expect(error.message.bytesize).to be < body.bytesize+ end+ end++ it "omits the description when the failed response has no body" do+ stub_request(:get, "https://api.example.com/empty").to_return(status: 503, body: "")++ config = { "method" => "GET", "url" => "https://api.example.com/empty" }++ expect { execute_node(configuration: config, item: item) }.to raise_error(+ DiscourseWorkflows::NodeError,+ "HTTP GET https://api.example.com/empty failed with status 503",)end