Skip to content

Commit 7ecf187

Browse files
author
Pavel Kosov
committed
[LNT] Add more information to error messages
Add some useful information to HTTP errors with codes 400 and 404 to make it easier to understand which argument is missing or incorrect. Reviewed By: cmatthews Differential Revision: https://reviews.llvm.org/D112525 OS Laboratory. Huawei Russian Research Institute. Saint-Petersburg
1 parent 976f1c4 commit 7ecf187

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

lnt/server/ui/views.py

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ def select_db():
8080
path = request.args.get('path')
8181
db = request.args.get('db')
8282
if path is None:
83-
abort(400)
83+
abort(400, "'path' argument is missing")
8484
if db not in current_app.old_config.databases:
85-
abort(404)
85+
abort(404, "'db' argument is missing or invalid")
8686

8787
# Rewrite the path.
8888
new_path = "/db_%s" % db
@@ -315,7 +315,7 @@ def v4_machine(id):
315315
try:
316316
machine = session.query(ts.Machine).filter(ts.Machine.id == id).one()
317317
except NoResultFound:
318-
abort(404)
318+
abort(404, "Invalid machine id {}".format(id))
319319

320320
if request.args.get('json'):
321321
json_obj = dict()
@@ -350,7 +350,7 @@ def __init__(self, run_id):
350350
self.ts = ts = request.get_testsuite()
351351
self.run = run = session.query(ts.Run).filter_by(id=run_id).first()
352352
if run is None:
353-
abort(404)
353+
abort(404, "Invalid run id {}".format(run_id))
354354

355355
# Get the aggregation function to use.
356356
aggregation_fn_name = request.args.get('aggregation_fn')
@@ -616,7 +616,7 @@ def v4_order(id):
616616
# Get the order.
617617
order = session.query(ts.Order).filter(ts.Order.id == id).first()
618618
if order is None:
619-
abort(404)
619+
abort(404, "Invalid order id {}".format(id))
620620

621621
previous_order = None
622622
if order.previous_order_id:
@@ -646,7 +646,7 @@ def v4_set_baseline(id):
646646
ts = request.get_testsuite()
647647
base = session.query(ts.Baseline).get(id)
648648
if not base:
649-
return abort(404)
649+
return abort(404, "Invalid baseline id {}".format(id))
650650
flash("Baseline set to " + base.name, FLASH_SUCCESS)
651651
flask.session[baseline_key(ts.name)] = id
652652

@@ -674,7 +674,7 @@ def v4_run_graph(id):
674674
ts = request.get_testsuite()
675675
run = session.query(ts.Run).filter_by(id=id).first()
676676
if run is None:
677-
abort(404)
677+
abort(404, "Invalid run id {}".format(id))
678678

679679
# Convert the old style test parameters encoding.
680680
args = {'highlight_run': id}
@@ -722,7 +722,7 @@ def v4_graph_for_sample(sample_id, field_name):
722722
ts = request.get_testsuite()
723723
target_sample = session.query(ts.Sample).get(sample_id)
724724
if not target_sample:
725-
abort(404, "Could not find sample id: {}".format(sample_id))
725+
abort(404, "Could not find sample id {}".format(sample_id))
726726

727727
# Get the field index we are interested in.
728728
field_index = None
@@ -814,19 +814,26 @@ def v4_graph():
814814
test_id = int(test_id_str)
815815
field_index = int(field_index_str)
816816
except ValueError:
817-
return abort(400)
817+
return abort(400, "Parameter {} was malformed. {} must be int.int.int" \
818+
.format(name, value))
818819

819820
if not (0 <= field_index < len(ts.sample_fields)):
820-
return abort(404)
821+
return abort(404, "Invalid field index {}".format(field_index))
821822

822823
try:
823824
machine = session.query(ts.Machine) \
824825
.filter(ts.Machine.id == machine_id) \
825826
.one()
827+
except NoResultFound:
828+
return abort(404, "Invalid machine id {}".format(machine_id))
829+
try:
826830
test = session.query(ts.Test).filter(ts.Test.id == test_id).one()
831+
except NoResultFound:
832+
return abort(404, "Invalid test id {}".format(test_id))
833+
try:
827834
field = ts.sample_fields[field_index]
828835
except NoResultFound:
829-
return abort(404)
836+
return abort(404, "Invalid field_index {}".format(field_index))
830837
graph_parameters.append(GraphParameter(machine, test, field, field_index))
831838

832839
# Order the plots by machine name, test name and then field.
@@ -848,17 +855,18 @@ def v4_graph():
848855
machine_id = int(machine_id_str)
849856
field_index = int(field_index_str)
850857
except ValueError:
851-
return abort(400)
858+
return abort(400, "Parameter {} was malformed. {} must be int.int" \
859+
.format(name, value))
852860

853861
if not (0 <= field_index < len(ts.sample_fields)):
854-
return abort(404)
862+
return abort(404, "Invalid field index {}".format(field_index))
855863

856864
try:
857865
machine = session.query(ts.Machine) \
858866
.filter(ts.Machine.id == machine_id) \
859867
.one()
860868
except NoResultFound:
861-
return abort(404)
869+
return abort(404, "Invalid machine id {}".format(machine_id))
862870
field = ts.sample_fields[field_index]
863871

864872
mean_parameter = (machine, field)
@@ -882,7 +890,7 @@ def v4_graph():
882890
try:
883891
run_id = int(run_id_str)
884892
except Exception:
885-
return abort(400)
893+
return abort(400, "Invalid baseline run id {}".format(run_id_str))
886894

887895
try:
888896
run = session.query(ts.Run) \
@@ -905,7 +913,7 @@ def v4_graph():
905913
highlight_run = session.query(ts.Run).filter_by(
906914
id=int(highlight_run_id)).first()
907915
if highlight_run is None:
908-
abort(404)
916+
abort(404, "Invalid highlight_run id {}".format(highlight_run_id))
909917

910918
# Find the neighboring runs, by order.
911919
prev_runs = list(ts.get_previous_runs_on_machine(session,
@@ -1708,32 +1716,32 @@ def v4_matrix():
17081716
test_id = int(test_id_str)
17091717
field_index = int(field_index_str)
17101718
except ValueError:
1711-
err_msg = "data {} was malformed. {} must be int.int.int"
1719+
err_msg = "Parameter {} was malformed. {} must be int.int.int"
17121720
return abort(400, err_msg.format(name, value))
17131721

17141722
if not (0 <= field_index < len(ts.sample_fields)):
1715-
return abort(404, "Invalid field index: {}".format(field_index))
1723+
return abort(404, "Invalid field index {}".format(field_index))
17161724

17171725
try:
17181726
machine = session.query(ts.Machine) \
17191727
.filter(ts.Machine.id == machine_id) \
17201728
.one()
17211729
except NoResultFound:
1722-
return abort(404, "Invalid machine ID: {}".format(machine_id))
1730+
return abort(404, "Invalid machine id {}".format(machine_id))
17231731
try:
17241732
test = session.query(ts.Test).filter(ts.Test.id == test_id).one()
17251733
except NoResultFound:
1726-
return abort(404, "Invalid test ID: {}".format(test_id))
1734+
return abort(404, "Invalid test id {}".format(test_id))
17271735
try:
17281736
field = ts.sample_fields[field_index]
17291737
except NoResultFound:
1730-
return abort(404, "Invalid field_index: {}".format(field_index))
1738+
return abort(404, "Invalid field_index {}".format(field_index))
17311739

17321740
valid_request = MatrixDataRequest(machine, test, field)
17331741
data_parameters.append(valid_request)
17341742

17351743
if not data_parameters:
1736-
abort(404, "Request requires some data arguments.")
1744+
abort(404, "Request requires some plot arguments.")
17371745
# Feature: if all of the results are from the same machine, hide the name
17381746
# to make the headers more compact.
17391747
dedup = True
@@ -1775,7 +1783,7 @@ def v4_matrix():
17751783
all_orders.add(s[1])
17761784
order_to_id[s[1]] = s[2]
17771785
if not all_orders:
1778-
abort(404, "No data found.")
1786+
abort(404, "No orders found.")
17791787
# Now grab the baseline data.
17801788
user_baseline = baseline()
17811789
backup_baseline = next(iter(all_orders))

tests/server/ui/test_matrix_page.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ def test_config_errors(self):
3636
client = self.client
3737
reply = check_code(client, '/v4/nts/matrix',
3838
expected_code=HTTP_NOT_FOUND)
39-
self.assertIn("Request requires some data arguments.",
39+
self.assertIn("Request requires some plot arguments.",
4040
reply.get_data(as_text=True))
4141

4242
reply = check_code(client, '/v4/nts/matrix?plot.0=1.1.1',
4343
expected_code=HTTP_NOT_FOUND)
44-
self.assertIn("No data found.", reply.get_data(as_text=True))
44+
self.assertIn("No orders found.", reply.get_data(as_text=True))
4545

4646
reply = check_code(client, '/v4/nts/matrix?plot.0=a.2.0',
4747
expected_code=HTTP_BAD_REQUEST)

0 commit comments

Comments
 (0)