Commit f31557c6 authored by Lennart Holzenkamp's avatar Lennart Holzenkamp
Browse files

refactoring error messages

parent 0966d9d7
import tempfile
from flask import Flask, request, jsonify, send_file
from venv import create
from flask import Flask, Response, request, jsonify, send_file
import os
import ocel_clustering.main as ocel_clustering
from werkzeug.utils import secure_filename
......@@ -22,6 +23,14 @@ def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def create_error_json(id: str, description: str) -> Response:
return jsonify({
'errors': [{
'id': id,
'description': description
}]
})
# returns ordered list of result-files without directory or anything.
# valid results cant be empty. if empty list is returned, there exeists no results.
def get_result_files_for(original_ocel_file_name: str) -> list[str]:
......@@ -77,52 +86,23 @@ def ocel_upload_delete(filename):
}
]
})
else:
return jsonify({
'errors': [
{
'name': 'file-not-found',
'description': 'The given file does not exist.'
}
]
}), 404
else: return create_error_json('file-not-found', 'The given file does not exist.'), 404
@app.route('/ocel-upload', methods=['POST'])
def ocel_upload_post():
# check if the post request has the file part
if 'file' not in request.files:
return jsonify({
'errors': [
{
'name': 'no-file-attribute',
'description': 'your request does not provide a file-attribute'
}
]
}), 404
return create_error_json('no-file-attribute', 'The request does not contain a file attribute.'), 404
file = request.files['file']
# If the user does not select a file, the browser submits an
# empty file without a filename.
if file.filename == '':
return jsonify({
'errors': [
{
'name': 'empty-file',
'description': 'your file is empty'
}
]
}), 404
return create_error_json('empty-filename', 'The filename of the given file is empty'), 404
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
full_filename = os.path.join(app.config['UPLOAD_FOLDER'], filename)
if(exists(full_filename)):
return jsonify({
'errors': [
{
'name': 'file-already-exists',
'description': 'The given file already exists. Use the same API-Endpoint with DELETE to delete the current one.'
}
]
}), 404
return create_error_json('file-already-exists', 'There exists already file with the given name. Use the file or delete it via the DELETE method.'), 404
file.save(full_filename)
return jsonify({
'data': {
......@@ -130,15 +110,7 @@ def ocel_upload_post():
'type': pathlib.Path(filename).suffix[1:]
}
})
else:
return jsonify({
'errors': [
{
'name': 'file-type-error',
'description': 'The given filetype is not allowed.'
}
]
}), 404
else: return create_error_json('file-type-error', 'The file type is not supported. Use one of: ' + ', '.join(ALLOWED_EXTENSIONS)), 404
# CLUSTERING-ROUTES
......@@ -148,15 +120,7 @@ def ocel_cluster_post(filename):
s_filename = secure_filename(filename)
full_filename = os.path.join(app.config['UPLOAD_FOLDER'], s_filename)
if not exists(full_filename) or not pathlib.Path(s_filename).suffix[1:] in ALLOWED_EXTENSIONS:
return jsonify({
'errors': [
{
'name': 'file-not-found',
'description': 'The file does not exist.'
}
]
}), 404
return create_error_json('file-not-found', 'The file does not exists.'), 404
try:
JSON_sent = request.get_json().get('data', {})
......@@ -216,22 +180,14 @@ def ocel_cluster_post(filename):
'data': result_files
})
except Exception as e:
return jsonify({
'errors': [{
'name': 'general-exception',
'description': str(e),
}]})
# this is NOT Best-practice, as with str(e) possibly server internal are published.
return create_error_json('unkown-exception', str(e)), 404
@app.route("/ocel-cluster/<filename>/clusters", methods=['GET'])
def ocel_cluster_get(filename):
s_filename = secure_filename(filename)
if not exists(app.config['UPLOAD_FOLDER'] + '/' + s_filename):
return jsonify({
'errors': [{
'name': 'not-found',
'description': 'not found. upload file first.'
}]
})
return create_error_json('file-not-found', 'The file does not exists.'), 404
suffix = get_suffix(s_filename)
file_list = sorted(get_result_files_for(s_filename))
......@@ -243,12 +199,7 @@ def ocel_cluster_get(filename):
})
if len(res) == 0:
return jsonify({
'errors': [{
'name': 'not-found',
'description': 'not found. execute POST first.'
}]
}), 404
return create_error_json('results-not-found', 'There are no results for the given file. Execute POST method first.'), 404
else:
return jsonify({
'data': res
......@@ -260,12 +211,7 @@ def ocel_cluster_get_result_file(filename, number):
number = int(number)
res_files = get_result_files_for(filename)
if number >= len(res_files) or number < 0:
return jsonify({
'errors': [{
'name': 'not-found',
'description': 'index out of bounds. cant be less than zero and cant be higher than result count.'
}]
}), 404
return create_error_json('index-out-of-bounds', 'There is no cluster with index ' + str(number) + '. Use a value in between 0 and ' + str(len(res_files)-1) + '.'), 404
full_filename = app.config['UPLOAD_FOLDER'] + '/' + filename + RESULT_FOLDER_SUFFIX + '/' + res_files[number]
text_file = open(full_filename, 'r')
......@@ -281,12 +227,7 @@ def ocel_cluster_get_visualization(filename, number):
s_filename = secure_filename(filename)
res_files = get_result_files_for(s_filename)
if number >= len(res_files) or number < 0:
return jsonify({
'errors': [{
'name': 'not-found',
'description': 'index out of bounds. cant be less than zero and cant be higher than result count.'
}]
}), 404
return create_error_json('index-out-of-bounds', 'There is no cluster with index ' + str(number) + '. Use a value in between 0 and ' + str(len(res_files)-1) + '.'), 404
full_filename = app.config['UPLOAD_FOLDER'] + '/' + s_filename + RESULT_FOLDER_SUFFIX + '/' + res_files[number]
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment