From a85683e9fbb1f9b924f03809f4ca3a59f2d82304 Mon Sep 17 00:00:00 2001 From: peter Date: Wed, 18 Dec 2019 09:02:58 +0100 Subject: [PATCH] patch: More verbose yaml errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the docstring is invalid yaml/swagger, the generated swagged documentation just shows: ``` Invalid Swagger ⚠ Swagger document could not be loaded from docstring ⚠ ``` This change makes the docstring parsing step a bit more verbose to indicate that there are problems. It is still not ideal but it is an improvement over guessing/ copy-pasta into a validator. How to test that this is working: 1) create a aiohttp route with docstring, add it to the app with the swagger plugin 2) start the app 3) navigate to the /api/doc page and find the route observe that the route is displayed correctly in the swagger page 4) change the docstring to invalid yaml (e.g. by indenting with tabs) 5) (re) start the app observe that there is more verbose output 6) (re) navigat ot the /api/doc page and find the route observe that the route now probably is labeled as 'Invalid Swagger' --- aiohttp_swagger/helpers/builders.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/aiohttp_swagger/helpers/builders.py b/aiohttp_swagger/helpers/builders.py index 5384be1..bac3531 100644 --- a/aiohttp_swagger/helpers/builders.py +++ b/aiohttp_swagger/helpers/builders.py @@ -29,7 +29,17 @@ def _extract_swagger_docs(end_point_doc, method="get"): end_point_swagger_doc = ( yaml.full_load("\n".join(end_point_doc[end_point_swagger_start:])) ) - except yaml.YAMLError: + except yaml.YAMLError as exc: + if hasattr(exc, 'problem_mark'): + if exc.context != None: + print (' parser says\n' + str(exc.problem_mark) + '\n ' + + str(exc.problem) + ' ' + str(exc.context) + + '\nPlease correct data and retry.') + else: + print (' parser says\n' + str(exc.problem_mark) + '\n ' + + str(exc.problem) + '\nPlease correct data and retry.') + else: + print ("Something went wrong while parsing yaml file") end_point_swagger_doc = { "description": "⚠ Swagger document could not be loaded " "from docstring ⚠",