From a68fe177ae41f2e58a64b3f8dcf3f825d004eeca Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Mon, 1 Jul 2024 14:59:32 -0500 Subject: Respond with JSON. Handle error messages on UI. --- .../rqtl2/upload-rqtl2-bundle-step-01.html | 15 +++++++++++++++ qc_app/upload/rqtl2.py | 22 ++++++++++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/qc_app/templates/rqtl2/upload-rqtl2-bundle-step-01.html b/qc_app/templates/rqtl2/upload-rqtl2-bundle-step-01.html index c6c79e5..07c240f 100644 --- a/qc_app/templates/rqtl2/upload-rqtl2-bundle-step-01.html +++ b/qc_app/templates/rqtl2/upload-rqtl2-bundle-step-01.html @@ -240,6 +240,21 @@ window.location.replace(uri); } }); + + r.on("error", (message, file) => { + filename = (file.webkitRelativePath + || file.relativePath + || file.fileName + || file.name); + jsonmsg = JSON.parse(message); + alert("There was an error while uploading your file '" + + filename + + "'. The error message was:\n\n\t" + + jsonmsg.error + + " (" + + jsonmsg.statuscode + + "): " + jsonmsg.message); + }) } else { setup_upload_handlers( "frm-upload-rqtl2-bundle", make_data_uploader( diff --git a/qc_app/upload/rqtl2.py b/qc_app/upload/rqtl2.py index 31d5a9d..e691636 100644 --- a/qc_app/upload/rqtl2.py +++ b/qc_app/upload/rqtl2.py @@ -13,11 +13,11 @@ import MySQLdb as mdb from redis import Redis from MySQLdb.cursors import DictCursor from werkzeug.utils import secure_filename -from werkzeug.exceptions import NotFound, BadRequest from flask import ( flash, escape, request, + jsonify, url_for, redirect, Response, @@ -254,13 +254,21 @@ def upload_rqtl2_bundle_chunked_get(# pylint: disable=["unused-argument"] filename = request.args.get("resumableFilename", type=str) or "" chunk = request.args.get("resumableChunkNumber", type=int) or 0 if not(fileid or filename or chunk): - raise BadRequest("At least one required query parameter is missing.") + return jsonify({ + "message": "At least one required query parameter is missing.", + "error": "BadRequest", + "statuscode": 400 + }), 400 if Path(chunks_directory(fileid), chunk_name(filename, chunk)).exists(): return "OK" - raise NotFound(description=f"Chunk {chunk} was not found.") + return jsonify({ + "message": f"Chunk {chunk} was not found.", + "error": "NotFound", + "statuscode": 404 + }), 404 def __merge_chunks__(targetfile: Path, chunkpaths: tuple[Path, ...]) -> Path: @@ -294,7 +302,13 @@ def upload_rqtl2_bundle_chunked_post(species_id: int, population_id: int): _targetfile = Path(app.config["UPLOAD_FOLDER"], _fileid) if _targetfile.exists(): - raise BadRequest("The file has already been uploaded.") + return jsonify({ + "message": ( + "A file with a similar unique identifier has previously been " + "uploaded and possibly is/has being/been processed."), + "error": "BadRequest", + "statuscode": 400 + }), 400 # save chunk data chunks_directory(_fileid).mkdir(exist_ok=True) -- cgit v1.2.3