abort(400) is not propagated correctly
The latest release of LabThings means abort(400)
now propagates an HTTPError
that should result in a sensible error message to the client. However, currently this fails:
[2021-08-23 09:50:27,673] [Thread-324] [INFO] Running tile scan...
[2021-08-23 09:50:27,719] [Thread-323] [ERROR] Exception on /api/v2/extensions/org.openflexure.smart-stack/tile [POST]
Traceback (most recent call last):
File "/var/openflexure/extensions/microscope_extensions/smart_stack/ofm_extension.py", line 122, in tile
os.makedirs(scan_dir)
File "/usr/lib/python3.7/os.py", line 221, in makedirs
mkdir(name, mode)
FileExistsError: [Errno 17] File exists: '/var/openflexure/data/micrographs/SMART_SCAN_ZR7P'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/var/openflexure/application/openflexure-microscope-server/.venv/lib/python3.7/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/var/openflexure/application/openflexure-microscope-server/.venv/lib/python3.7/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/var/openflexure/application/openflexure-microscope-server/.venv/lib/python3.7/site-packages/flask/views.py", line 89, in view
return self.dispatch_request(*args, **kwargs)
File "/var/openflexure/application/openflexure-microscope-server/.venv/lib/python3.7/site-packages/labthings/views/__init__.py", line 250, in dispatch_request
raise task.exception
File "/var/openflexure/application/openflexure-microscope-server/.venv/lib/python3.7/site-packages/labthings/actions/thread.py", line 255, in wrapped
self._return_value = f(*args, **kwargs)
File "/var/openflexure/application/openflexure-microscope-server/.venv/lib/python3.7/site-packages/flask/ctx.py", line 158, in wrapper
return f(*args, **kwargs)
File "/var/openflexure/application/openflexure-microscope-server/.venv/lib/python3.7/site-packages/webargs/core.py", line 450, in wrapper
return func(*args, **kwargs)
File "/var/openflexure/extensions/microscope_extensions/smart_stack/ofm_extension.py", line 301, in post
return self.extension.tile(microscope, **kwargs)
File "/var/openflexure/extensions/microscope_extensions/smart_stack/ofm_extension.py", line 124, in tile
abort(400, "The filename you specified already exists.")
File "/var/openflexure/application/openflexure-microscope-server/.venv/lib/python3.7/site-packages/werkzeug/exceptions.py", line 822, in abort
return _aborter(status, *args, **kwargs)
File "/var/openflexure/application/openflexure-microscope-server/.venv/lib/python3.7/site-packages/werkzeug/exceptions.py", line 807, in __call__
raise self.mapping[code](*args, **kwargs)
werkzeug.exceptions.BadRequest: 400 Bad Request: The filename you specified already exists.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/var/openflexure/application/openflexure-microscope-server/.venv/lib/python3.7/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/var/openflexure/application/openflexure-microscope-server/.venv/lib/python3.7/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/var/openflexure/application/openflexure-microscope-server/.venv/lib/python3.7/site-packages/flask_cors/extension.py", line 165, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/var/openflexure/application/openflexure-microscope-server/.venv/lib/python3.7/site-packages/flask_cors/extension.py", line 165, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/var/openflexure/application/openflexure-microscope-server/.venv/lib/python3.7/site-packages/flask/app.py", line 1816, in handle_user_exception
return self.handle_http_exception(e)
File "/var/openflexure/application/openflexure-microscope-server/.venv/lib/python3.7/site-packages/flask/app.py", line 1744, in handle_http_exception
return handler(e)
File "/var/openflexure/application/openflexure-microscope-server/openflexure_microscope/api/app.py", line 117, in handle_error
headers = err.data.get("headers", None)
AttributeError: 'BadRequest' object has no attribute 'data'
I think the solution here is to fix handle_error
. If we do that, hopefully the error code will propagate nicely.