Skip to content

Commit

Permalink
Merge pull request #650 from replicatedhq/divolgin/sc-107722/install-…
Browse files Browse the repository at this point in the history
…scripts-api-returns-500s-when-they

Return 400 on bad requests
  • Loading branch information
divolgin authored Jun 28, 2024
2 parents 0065e69 + 81f6b90 commit eb9b0ae
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
23 changes: 23 additions & 0 deletions install_scripts/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,29 @@

_images = images.get_default_images()


@app.errorhandler(Exception)
def handle_error(e):
if isinstance(e, helpers.BadRequestException):
return str(e.message), 400
return "Internal Server Error", 500


@app.before_request
def validate_args():
query_args = {
k: v[0] if isinstance(v, list) and len(v) > 0 else v
for k, v in dict(request.args).items()
}
version_keys = ['replicated_tag', 'replicated_ui_tag', 'replicated_operator_tag',
'current_replicated_version', 'next_replicated_version', 'pinned_docker_version',
'kubernetes_version', 'docker_version']
for key in version_keys:
if key in query_args:
if semver.parse(query_args[key], loose=False) is None:
raise helpers.BadRequestException('Invalid value for {}: {}'.format(key, query_args[key]))


@app.teardown_appcontext
def teardown_db(exception):
db.teardown()
Expand Down
9 changes: 7 additions & 2 deletions install_scripts/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
_images = images.get_images()


class BadRequestException(Exception):
def __init__(self, message):
self.message = message


def template_args(**kwargs):
env = param.lookup('ENVIRONMENT', default='production')
args = {
Expand Down Expand Up @@ -192,7 +197,7 @@ def get_best_version(arg_name, default_arg_name, replicated_channel, app_slug,
elif is_valid_replicated_version(arg_version, replicated_channel,
scheduler=scheduler):
return arg_version
raise Exception('version {} does not exist'.format(arg_version))
raise BadRequestException('version {} does not exist'.format(arg_version))

return get_current_replicated_version(replicated_channel,
scheduler=scheduler)
Expand Down Expand Up @@ -333,7 +338,7 @@ def get_current_replicated_version(replicated_channel, scheduler=None):
row = cursor.fetchone()
cursor.close()
if row is None:
raise Exception('no releases for product {} and channel {}'.format(
raise BadRequestException('no releases for product {} and channel {}'.format(
product, replicated_channel))
(version, ) = row

Expand Down

0 comments on commit eb9b0ae

Please sign in to comment.