From 413f9af5688609423825a7df51fa9e17e0ff1d84 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Tue, 26 Oct 2021 12:18:55 -0700 Subject: [PATCH] Replace some flask.abort with exc.jsonify responses. --- .../ycdl_flask/backend/endpoints/channel_endpoints.py | 10 ++++------ .../ycdl_flask/backend/endpoints/video_endpoints.py | 8 ++++---- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/frontends/ycdl_flask/backend/endpoints/channel_endpoints.py b/frontends/ycdl_flask/backend/endpoints/channel_endpoints.py index caf14ea..e22279c 100644 --- a/frontends/ycdl_flask/backend/endpoints/channel_endpoints.py +++ b/frontends/ycdl_flask/backend/endpoints/channel_endpoints.py @@ -102,12 +102,10 @@ def get_watch(): videos = [video] return _render_videos_listing(videos, channel=None, state=None, orderby=None) +@flasktools.required_fields(['channel_id'], forbid_whitespace=True) @site.route('/add_channel', methods=['POST']) def post_add_channel(): - channel_id = request.form.get('channel_id', '') - channel_id = channel_id.strip() - if not channel_id: - flask.abort(400) + channel_id = request.form.['channel_id'] if not (len(channel_id) == 24 and channel_id.startswith('UC')): # It seems they have given us a username instead. try: @@ -156,8 +154,8 @@ def post_set_automark(channel_id): try: channel.set_automark(state, commit=True) - except ycdl.exceptions.InvalidVideoState: - flask.abort(400) + except ycdl.exceptions.InvalidVideoState as exc: + return flasktools.json_response(exc.jsonify(), status=400) response = {'id': channel.id, 'automark': channel.automark} return flasktools.json_response(response) diff --git a/frontends/ycdl_flask/backend/endpoints/video_endpoints.py b/frontends/ycdl_flask/backend/endpoints/video_endpoints.py index 3eaa248..225ac67 100644 --- a/frontends/ycdl_flask/backend/endpoints/video_endpoints.py +++ b/frontends/ycdl_flask/backend/endpoints/video_endpoints.py @@ -20,13 +20,13 @@ def post_mark_video_state(): video.mark_state(state, commit=False) common.ycdldb.commit() - except ycdl.exceptions.NoSuchVideo: + except ycdl.exceptions.NoSuchVideo as exc: common.ycdldb.rollback() - flask.abort(404) + return flasktools.json_response(exc.jsonify(), status=404) - except ycdl.exceptions.InvalidVideoState: + except ycdl.exceptions.InvalidVideoState as exc: common.ycdldb.rollback() - flask.abort(400) + return flasktools.json_response(exc.jsonify(), status=400) return flasktools.json_response({'video_ids': video_ids, 'state': state})