From c10b972ae2ec359237f6e39b80b10d6b611c4a7d Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Thu, 6 Feb 2020 18:07:46 +0100 Subject: [PATCH] views: improve jsonchema validation error message (#38338) --- passerelle/views.py | 5 ++++- tests/test_atal.py | 2 +- tests/test_cmis.py | 10 +++++----- tests/test_generic_endpoint.py | 2 +- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/passerelle/views.py b/passerelle/views.py index c4d9f58f..109a74de 100644 --- a/passerelle/views.py +++ b/passerelle/views.py @@ -363,7 +363,10 @@ class GenericEndpointView(GenericConnectorMixin, SingleObjectMixin, View): try: validate(data, json_schema) except ValidationError as e: - raise APIError(e.message, http_status=400) + error_msg = e.message + if e.path: + error_msg = '%s: %s' % ('/'.join(map(str, e.path)), error_msg) + raise APIError(error_msg, http_status=400) d['post_data'] = data return d diff --git a/tests/test_atal.py b/tests/test_atal.py index 6f89fc64..f038d864 100644 --- a/tests/test_atal.py +++ b/tests/test_atal.py @@ -189,7 +189,7 @@ def test_upload(app, connector, monkeypatch): 'data': None, 'err': 1, 'err_class': 'passerelle.utils.jsonresponse.APIError', - 'err_desc': "'content' is a required property" + 'err_desc': "file: 'content' is a required property" } # no file diff --git a/tests/test_cmis.py b/tests/test_cmis.py index 07817721..42c7a30c 100644 --- a/tests/test_cmis.py +++ b/tests/test_cmis.py @@ -103,7 +103,7 @@ def test_uploadfile_error_if_non_string_file_name(app, setup): expect_errors=True) assert response.status_code == 400 assert response.json['err'] == 1 - assert response.json['err_desc'] == "1 is not of type 'string'" + assert response.json['err_desc'] == "file/filename: 1 is not of type 'string'" response = app.post_json( '/cmis/slug-cmis/uploadfile', @@ -114,7 +114,7 @@ def test_uploadfile_error_if_non_string_file_name(app, setup): expect_errors=True) assert response.status_code == 400 assert response.json['err'] == 1 - assert response.json['err_desc'] == "1 is not of type 'string'" + assert response.json['err_desc'] == "filename: 1 is not of type 'string'" def test_uploadfile_error_if_non_valid_file_name(app, setup): @@ -160,7 +160,7 @@ def test_uploadfile_error_if_non_string_path(app, setup): expect_errors=True) assert response.status_code == 400 assert response.json['err'] == 1 - assert response.json['err_desc'] == "1 is not of type 'string'" + assert response.json['err_desc'] == "path: 1 is not of type 'string'" def test_uploadfile_error_if_no_regular_path(app, setup): @@ -183,7 +183,7 @@ def test_uploadfile_error_if_no_file_content(app, setup): expect_errors=True) assert response.status_code == 400 assert response.json['err'] == 1 - assert response.json['err_desc'] == "'content' is a required property" + assert response.json['err_desc'] == "file: 'content' is a required property" def test_uploadfile_error_if_non_string_file_content(app, setup): @@ -194,7 +194,7 @@ def test_uploadfile_error_if_non_string_file_content(app, setup): expect_errors=True) assert response.status_code == 400 assert response.json['err'] == 1 - assert response.json['err_desc'] == "1 is not of type 'string'" + assert response.json['err_desc'] == "file/content: 1 is not of type 'string'" def test_uploadfile_error_if_no_proper_base64_encoding(app, setup): diff --git a/tests/test_generic_endpoint.py b/tests/test_generic_endpoint.py index 209d1030..13e1440c 100644 --- a/tests/test_generic_endpoint.py +++ b/tests/test_generic_endpoint.py @@ -380,7 +380,7 @@ def test_endpoint_decorator_pre_process(db, app): with patch_init, patch_object: resp = app.post_json(url_foo, params=payload, status=400) assert resp.json['err'] == 1 - assert resp.json['err_desc'] == "None is not of type %s" % repr(u'integer') + assert resp.json['err_desc'] == "foo/1/id: None is not of type %s" % repr(u'integer') with patch_init, patch_object: resp = app.post_json(url_bar, params=payload) assert resp.json['err'] == 0 -- 2.20.1