From 06f9b4be6943e6869933a5140db7ffc8a86094d0 Mon Sep 17 00:00:00 2001 From: Robert Barrow Date: Tue, 4 Jun 2019 19:51:24 +0100 Subject: [PATCH] fix for empty api_body and addition of catch for bad JSON --- lib/builder.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/builder.rb b/lib/builder.rb index bc873261..654ff58b 100644 --- a/lib/builder.rb +++ b/lib/builder.rb @@ -394,7 +394,18 @@ class CustomWizard::Builder end def send_to_api(user, action, data) - api_body = CustomWizard::Builder.fill_placeholders(JSON.generate(JSON.parse(action['api_body'])), user, data) + + api_body = nil + + if action['api_body'] != "" + begin + api_body_parsed = JSON.parse(action['api_body']) + rescue + raise Discourse::InvalidParameters, "Invalid API body definition: #{action['api_body']} for #{action['title']}" + end + api_body = CustomWizard::Builder.fill_placeholders(JSON.generate(api_body_parsed), user, data) + end + result = CustomWizard::Api::Endpoint.request(action['api'], action['api_endpoint'], api_body) if result['error']