From abb39d06a20e7e3fb2b344980d79b59e37357c1e Mon Sep 17 00:00:00 2001 From: epriestley Date: Fri, 14 Oct 2011 10:50:47 -0700 Subject: [PATCH] Provide a better error message when a user enters a Conduit parameter string without quotes around it (and similar) Summary: See D1010. The API uniformly requires JSON, which is good for strictness and predictablity but can be bad for UEX, especially considering that we silently continue after failing to decode things. Toss the user a lifeline when they make this common mistake. Test Plan: Ran API calls with invalid and valid inputs. Invalid inputs gave me a reasonable error message. Reviewers: davidreuss, jungejason, nh, tuomaspelkonen, aran Reviewed By: nh CC: aran, nh Differential Revision: 1012 --- .../api/PhabricatorConduitAPIController.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/applications/conduit/controller/api/PhabricatorConduitAPIController.php b/src/applications/conduit/controller/api/PhabricatorConduitAPIController.php index bb5ddffed2..bfbc788321 100644 --- a/src/applications/conduit/controller/api/PhabricatorConduitAPIController.php +++ b/src/applications/conduit/controller/api/PhabricatorConduitAPIController.php @@ -67,7 +67,22 @@ class PhabricatorConduitAPIController if (isset($_REQUEST['params']) && is_array($_REQUEST['params'])) { $params_post = $request->getArr('params'); foreach ($params_post as $key => $value) { - $params_post[$key] = json_decode($value, true); + $decoded_value = json_decode($value, true); + if ($decoded_value === null && strtolower($value) != 'null') { + // When json_decode() fails, it returns null. This almost certainly + // indicates that a user was using the web UI and didn't put quotes + // around a string value. We can either do what we think they meant + // (treat it as a string) or fail. For now, err on the side of + // caution and fail. In the future, if we make the Conduit API + // actually do type checking, it might be reasonable to treat it as + // a string if the parameter type is string. + throw new Exception( + "The value for parameter '{$key}' is not valid JSON. All ". + "parameters must be encoded as JSON values, including strings ". + "(which means you need to surround them in double quotes). ". + "Check your syntax. Value was: {$value}"); + } + $params_post[$key] = $decoded_value; } $params = $params_post; } else {