diff --git a/openflexure_microscope/api/utilities.py b/openflexure_microscope/api/utilities.py index cfe9ea20..18acca49 100644 --- a/openflexure_microscope/api/utilities.py +++ b/openflexure_microscope/api/utilities.py @@ -1,6 +1,7 @@ import pprint +import logging import copy - +from werkzeug.exceptions import BadRequest class JsonPayload: def __init__(self, request): @@ -8,7 +9,11 @@ class JsonPayload: Object to wrap up simple functionality for parsing a JSON response. """ # Try to load as json - self.json = request.get_json() #: dict: Dictionary representation of request JSON + try: + self.json = request.get_json() #: dict: Dictionary representation of request JSON + except BadRequest as e: + logging.error(e) + self.json = {} if self.json is None: self.json = {} diff --git a/openflexure_microscope/api/v1/blueprints/task.py b/openflexure_microscope/api/v1/blueprints/task.py index 6e08c5ac..173ca2b2 100644 --- a/openflexure_microscope/api/v1/blueprints/task.py +++ b/openflexure_microscope/api/v1/blueprints/task.py @@ -43,6 +43,8 @@ class TaskListAPI(MicroscopeView): :>header Content-Type: application/json """ + print(self.microscope.task.state) + data = self.microscope.task.state return jsonify(data) diff --git a/openflexure_microscope/plugins/example/api.py b/openflexure_microscope/plugins/example/api.py index f3da64ff..f6f38cf7 100644 --- a/openflexure_microscope/plugins/example/api.py +++ b/openflexure_microscope/plugins/example/api.py @@ -72,5 +72,24 @@ class LongRunningAPI(MicroscopeViewPlugin): task = self.microscope.task.start(self.plugin.long_running, time_to_run) return jsonify(task.state), 202 + except TaskDeniedException: + return abort(409) + +class SomeExceptionAPI(MicroscopeViewPlugin): + """ + An example API plugin that uses a long-running but broken plugin method. + """ + def post(self): + """ + Method to call when an HTTP POST request is made. + """ + # Get payload JSON + payload = JsonPayload(request) + + # Attach the long-running method as a microscope task + try: + task = self.microscope.task.start(self.plugin.some_exception) + return jsonify(task.state), 202 + except TaskDeniedException: return abort(409) \ No newline at end of file diff --git a/openflexure_microscope/plugins/example/plugin.py b/openflexure_microscope/plugins/example/plugin.py index a7e0c044..9fff76aa 100644 --- a/openflexure_microscope/plugins/example/plugin.py +++ b/openflexure_microscope/plugins/example/plugin.py @@ -2,7 +2,7 @@ import random import time from openflexure_microscope.plugins import MicroscopePlugin -from .api import IdentifyAPI, HelloWorldAPI, LongRunningAPI +from .api import IdentifyAPI, HelloWorldAPI, LongRunningAPI, SomeExceptionAPI class Plugin(MicroscopePlugin): @@ -14,6 +14,7 @@ class Plugin(MicroscopePlugin): '/identify': IdentifyAPI, '/hello': HelloWorldAPI, '/long_running': LongRunningAPI, + '/some_exception': SomeExceptionAPI, } def identify(self): @@ -32,6 +33,16 @@ class Plugin(MicroscopePlugin): return "Hello world!" + def some_exception(self): + """ + Demonstrate some broken plugin method that would be long running + """ + with self.microscope.lock: + time.sleep(3) + result = 15./0 + + return result + def long_running(self, t_run): """ Demonstrate a long-running method that requires microscope hardware diff --git a/openflexure_microscope/task.py b/openflexure_microscope/task.py index c391aace..3f75f73c 100644 --- a/openflexure_microscope/task.py +++ b/openflexure_microscope/task.py @@ -1,6 +1,8 @@ from threading import Thread from functools import wraps import datetime +import logging +import traceback import time import uuid @@ -111,7 +113,9 @@ class Task: r = f(*args, **kwargs) s = 'success' except Exception as e: - r = e + logging.error(e) + logging.error(traceback.format_exc()) + r = str(e) s = 'error' self.state['return'] = r self.state['status'] = s