Add error IDs to things that can go wrong when running a compose.

Note the exception string checking around compose_type.  I didn't really
want to introduce a new exception type just for this, but also didn't
want to duplicate strings.  I'd be open to other suggestions for how to
do this.

(cherry picked from commit b3bb438254)
This commit is contained in:
Chris Lumens 2018-08-09 10:21:21 -04:00
parent c1af7dd69d
commit 6a3ccd6ade
3 changed files with 18 additions and 4 deletions

View File

@ -16,10 +16,17 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# 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"

View File

@ -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)

View File

@ -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):