Max Reimann commited on
Commit
1834769
1 Parent(s): c7937b8

improve error handling

Browse files
Files changed (1) hide show
  1. worker/serve.py +32 -30
worker/serve.py CHANGED
@@ -28,31 +28,7 @@ from parameter_optimization.strotss_org import strotss, pil_resize_long_edge_to
28
  from helpers import torch_to_np, np_to_torch
29
  from effects import get_default_settings, MinimalPipelineEffect
30
 
31
- class JSONExceptionHandler(object):
32
-
33
- def __init__(self, app=None):
34
- if app:
35
- self.init_app(app)
36
-
37
- def std_handler(self, error):
38
- response = jsonify(message=error.message)
39
- response.status_code = error.code if isinstance(error, HTTPException) else 500
40
- return response
41
-
42
-
43
- def init_app(self, app):
44
- self.app = app
45
- self.register(HTTPException)
46
- for code, v in default_exceptions.items():
47
- self.register(code)
48
-
49
- def register(self, exception_or_code, handler=None):
50
- self.app.errorhandler(exception_or_code)(handler or self.std_handler)
51
-
52
-
53
-
54
  app = Flask(__name__)
55
- handler = JSONExceptionHandler(app)
56
 
57
  image_folder = 'img_received'
58
  photos = UploadSet('photos', IMAGES)
@@ -210,6 +186,26 @@ class StylerQueue:
210
 
211
  styler_queue = StylerQueue()
212
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213
 
214
  @app.route('/upload', methods=['POST'])
215
  def upload():
@@ -223,15 +219,17 @@ def upload():
223
  print('added new stylization task', style_filename, content_filename)
224
 
225
  return jsonify({"task_id": job_id})
226
- abort(jsonify(message="request needs style, content image"), 400)
227
 
228
  @app.route('/get_status')
229
  def get_status():
 
 
230
  task_id = int(request.args.get("task_id"))
231
  task = styler_queue.get_task(task_id)
232
 
233
  if task is None:
234
- abort(jsonify(message="task with id %d not found"%task_id), 400)
235
 
236
  status = {
237
  "status": task.status,
@@ -259,27 +257,31 @@ def get_queue_length():
259
 
260
  @app.route('/get_image')
261
  def get_image():
 
 
262
  task_id = int(request.args.get("task_id"))
263
  task = styler_queue.get_task(task_id)
264
 
265
  if task is None:
266
- abort(jsonify(message="task with id %d not found"%task_id), 400)
267
 
268
  if task.status != "finished":
269
- abort(jsonify(message="task with id %d not in finished state"%task_id), 400)
270
 
271
  return send_file(os.path.join(image_folder, task.output_filename), mimetype='image/jpg')
272
 
273
  @app.route('/get_vp')
274
  def get_vp():
 
 
275
  task_id = int(request.args.get("task_id"))
276
  task = styler_queue.get_task(task_id)
277
 
278
  if task is None:
279
- abort(jsonify(message="task with id %d not found"%task_id), 400)
280
 
281
  if task.status != "finished":
282
- abort(jsonify(message="task with id %d not in finished state"%task_id), 400)
283
 
284
  return send_file(os.path.join(image_folder, task.vp_output_filename), mimetype='application/zip')
285
 
 
28
  from helpers import torch_to_np, np_to_torch
29
  from effects import get_default_settings, MinimalPipelineEffect
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  app = Flask(__name__)
 
32
 
33
  image_folder = 'img_received'
34
  photos = UploadSet('photos', IMAGES)
 
186
 
187
  styler_queue = StylerQueue()
188
 
189
+ @app.errorhandler(404)
190
+ def resource_not_found(e):
191
+ return jsonify(message=str(e)), 404
192
+
193
+ @app.errorhandler(500)
194
+ def internal_server_error(e):
195
+ return jsonify(message=str(e)), 500
196
+
197
+ @app.errorhandler(400)
198
+ def caught_error(e, *args):
199
+ print(args)
200
+ print(e)
201
+ return jsonify(message=str(e.description)), 400
202
+
203
+ @app.route('/', defaults={'path': ''})
204
+ @app.route('/<path:path>')
205
+ def catch_all(path):
206
+ abort(404, "route not found")
207
+
208
+
209
 
210
  @app.route('/upload', methods=['POST'])
211
  def upload():
 
219
  print('added new stylization task', style_filename, content_filename)
220
 
221
  return jsonify({"task_id": job_id})
222
+ abort(400, description="request needs style, content image")
223
 
224
  @app.route('/get_status')
225
  def get_status():
226
+ if request.args.get("task_id") is None:
227
+ abort(400, description="task_id needs to be supplied as parameter")
228
  task_id = int(request.args.get("task_id"))
229
  task = styler_queue.get_task(task_id)
230
 
231
  if task is None:
232
+ abort(400, description="task with id %d not found"%task_id)
233
 
234
  status = {
235
  "status": task.status,
 
257
 
258
  @app.route('/get_image')
259
  def get_image():
260
+ if request.args.get("task_id") is None:
261
+ abort(400, description="task_id needs to be supplied as parameter")
262
  task_id = int(request.args.get("task_id"))
263
  task = styler_queue.get_task(task_id)
264
 
265
  if task is None:
266
+ abort(400, description="task with id %d not found"%task_id)
267
 
268
  if task.status != "finished":
269
+ abort(400, description="task with id %d not in finished state"%task_id)
270
 
271
  return send_file(os.path.join(image_folder, task.output_filename), mimetype='image/jpg')
272
 
273
  @app.route('/get_vp')
274
  def get_vp():
275
+ if request.args.get("task_id") is None:
276
+ abort(400, description="task_id needs to be supplied as parameter")
277
  task_id = int(request.args.get("task_id"))
278
  task = styler_queue.get_task(task_id)
279
 
280
  if task is None:
281
+ abort(400, description="task with id %d not found"%task_id)
282
 
283
  if task.status != "finished":
284
+ abort(400, description="task with id %d not in finished state"%task_id)
285
 
286
  return send_file(os.path.join(image_folder, task.vp_output_filename), mimetype='application/zip')
287