From 672708e68d4125657f5e0f9c200b3f4d7fecdc48 Mon Sep 17 00:00:00 2001 From: Chris Lumens Date: Wed, 8 Aug 2018 10:22:22 -0400 Subject: [PATCH] Change the error return type for bad limit= and offset=. Each element in the errors value is now a dict, with a msg field and an id field. The id field contains a value out of errors.py that can be used by the front end to key on. The msg field is the same as what's been there. The idea is to keep the number of IDs somewhat limited so there's not a huge number of things for the front end to know. (cherry picked from commit 9677b012daa78f73e7c75e67254eb712c9298445) --- src/pylorax/api/errors.py | 21 +++++++++++++++++++++ src/pylorax/api/v0.py | 9 +++++---- 2 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 src/pylorax/api/errors.py diff --git a/src/pylorax/api/errors.py b/src/pylorax/api/errors.py new file mode 100644 index 00000000..77d7f5e2 --- /dev/null +++ b/src/pylorax/api/errors.py @@ -0,0 +1,21 @@ +# +# lorax-composer API server +# +# Copyright (C) 2018 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Returned from the API when ?limit= or ?offset= is given something that does +# not convert into an integer. +BAD_LIMIT_OR_OFFSET = "BadLimitOrOffset" diff --git a/src/pylorax/api/v0.py b/src/pylorax/api/v0.py index ca7b983e..4664f7ba 100644 --- a/src/pylorax/api/v0.py +++ b/src/pylorax/api/v0.py @@ -983,6 +983,7 @@ from pylorax.sysutils import joinpaths from pylorax.api.checkparams import checkparams from pylorax.api.compose import start_build, compose_types from pylorax.api.crossdomain import crossdomain +from pylorax.api.errors import * # pylint: disable=wildcard-import from pylorax.api.projects import projects_list, projects_info, projects_depsolve from pylorax.api.projects import modules_list, modules_info, ProjectsError, repo_to_source from pylorax.api.projects import get_repo_sources, delete_repo_source, source_to_repo, dnf_repo_to_file_repo @@ -1024,7 +1025,7 @@ def v0_api(api): limit = int(request.args.get("limit", "20")) offset = int(request.args.get("offset", "0")) except ValueError as e: - return jsonify(status=False, errors=[str(e)]), 400 + return jsonify(status=False, errors=[{"id": BAD_LIMIT_OR_OFFSET, "msg": str(e)}]), 400 with api.config["GITLOCK"].lock: blueprints = take_limits([f[:-5] for f in list_branch_files(api.config["GITLOCK"].repo, branch)], offset, limit) @@ -1113,7 +1114,7 @@ def v0_api(api): limit = int(request.args.get("limit", "20")) offset = int(request.args.get("offset", "0")) except ValueError as e: - return jsonify(status=False, errors=[str(e)]), 400 + return jsonify(status=False, errors=[{"id": BAD_LIMIT_OR_OFFSET, "msg": str(e)}]), 400 blueprints = [] errors = [] @@ -1465,7 +1466,7 @@ def v0_api(api): limit = int(request.args.get("limit", "20")) offset = int(request.args.get("offset", "0")) except ValueError as e: - return jsonify(status=False, errors=[str(e)]), 400 + return jsonify(status=False, errors=[{"id": BAD_LIMIT_OR_OFFSET, "msg": str(e)}]), 400 try: with api.config["DNFLOCK"].lock: @@ -1663,7 +1664,7 @@ def v0_api(api): limit = int(request.args.get("limit", "20")) offset = int(request.args.get("offset", "0")) except ValueError as e: - return jsonify(status=False, errors=[str(e)]), 400 + return jsonify(status=False, errors=[{"id": BAD_LIMIT_OR_OFFSET, "msg": str(e)}]), 400 if module_names: module_names = module_names.split(",")