diff --git a/src/pylorax/api/errors.py b/src/pylorax/api/errors.py index 7509a25e..d6a90e8f 100644 --- a/src/pylorax/api/errors.py +++ b/src/pylorax/api/errors.py @@ -16,10 +16,17 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +# Returned from the API when either an invalid compose type is given, or not +# compose type is given. +BAD_COMPOSE_TYPE = "BadComposeType" + # Returned from the API when ?limit= or ?offset= is given something that does # not convert into an integer. BAD_LIMIT_OR_OFFSET = "BadLimitOrOffset" +# Returned from the API for any other error resulting from /compose failing. +BUILD_FAILED = "BuildFailed" + # Returned from the API when it expected a build to be in a state other than # what it currently is. This most often happens when asking for results from # a build that is not yet done. @@ -29,6 +36,10 @@ BUILD_IN_WRONG_STATE = "BuildInWrongState" # given that contains invalid characters. INVALID_NAME = "InvalidName" +# Returned from the API when /compose is called without the POST body telling it +# what to compose. +MISSING_POST = "MissingPost" + # Returned from the API when someone tries to modify an immutable system source. SYSTEM_SOURCE = "SystemSource" diff --git a/src/pylorax/api/v0.py b/src/pylorax/api/v0.py index 48e0824d..f362927e 100644 --- a/src/pylorax/api/v0.py +++ b/src/pylorax/api/v0.py @@ -1751,7 +1751,7 @@ def v0_api(api): errors = [] if not compose: - return jsonify(status=False, errors=["Missing POST body"]), 400 + return jsonify(status=False, errors=[{"id": MISSING_POST, "msg": "Missing POST body"}]), 400 if "blueprint_name" not in compose: errors.append({"id": UNKNOWN_BLUEPRINT,"msg": "No 'blueprint_name' in the JSON request"}) @@ -1764,7 +1764,7 @@ def v0_api(api): branch = compose["branch"] if "compose_type" not in compose: - errors.append("No 'compose_type' in the JSON request") + errors.append({"id": BAD_COMPOSE_TYPE, "msg": "No 'compose_type' in the JSON request"}) else: compose_type = compose["compose_type"] @@ -1778,7 +1778,10 @@ def v0_api(api): build_id = start_build(api.config["COMPOSER_CFG"], api.config["DNFLOCK"], api.config["GITLOCK"], branch, blueprint_name, compose_type, test_mode) except Exception as e: - return jsonify(status=False, errors=[str(e)]), 400 + if "Invalid compose type" in str(e): + return jsonify(status=False, errors=[{"id": BAD_COMPOSE_TYPE, "msg": str(e)}]), 400 + else: + return jsonify(status=False, errors=[{"id": BUILD_FAILED, "msg": str(e)}]), 400 return jsonify(status=True, build_id=build_id) diff --git a/tests/pylorax/test_server.py b/tests/pylorax/test_server.py index 2f10bb6b..53292736 100644 --- a/tests/pylorax/test_server.py +++ b/tests/pylorax/test_server.py @@ -780,7 +780,7 @@ class ServerTestCase(unittest.TestCase): data = json.loads(resp.data) self.assertNotEqual(data, None) self.assertEqual(data["status"], False, "Failed to fail to start test compose: %s" % data) - self.assertEqual(data["errors"], ["Invalid compose type (snakes), must be one of ['ext4-filesystem', 'live-iso', 'partitioned-disk', 'qcow2', 'tar']"], + self.assertEqual(data["errors"], [{"id": BAD_COMPOSE_TYPE, "msg": "Invalid compose type (snakes), must be one of ['ext4-filesystem', 'live-iso', 'partitioned-disk', 'qcow2', 'tar']"}], "Failed to get errors: %s" % data) def test_compose_03_status_fail(self):