aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRahiel Kasim <rahielkasim@gmail.com>2017-06-15 15:00:17 +0200
committerRahiel Kasim <rahielkasim@gmail.com>2017-06-15 15:00:17 +0200
commit3f439d0b85dc92cb2c05f80337f7537daaa3009c (patch)
treed48b06e66e5107e4024e7a353d1825c32686a558
parentc0d5782e785c5abf317b4d3f5779a58f0822ed99 (diff)
better error handling/reporting
-rw-r--r--README.md10
-rw-r--r--api.py10
2 files changed, 18 insertions, 2 deletions
diff --git a/README.md b/README.md
index 5d6f933..1f15e17 100644
--- a/README.md
+++ b/README.md
@@ -49,3 +49,13 @@ import requests
r = requests.post("http://localhost:8080", data={"url": "http://example.com/image.jpg"})
nsfw_prob = float(r.text)
```
+
+# HTTP Errors
+
+## 415 Unsupported Media Type: Invalid image
+
+The requested `url` is not a valid image.
+
+## 400 Bad Request: Missing `url` POST parameter
+
+You need to specify `url` as a POST parameter.
diff --git a/api.py b/api.py
index de3c888..39a4937 100644
--- a/api.py
+++ b/api.py
@@ -5,6 +5,7 @@ import async_timeout
import numpy as np
import uvloop
from aiohttp import web
+from aiohttp.web import HTTPBadRequest, HTTPUnsupportedMediaType
from classify_nsfw import caffe_preprocess_and_compute, load_model
@@ -30,8 +31,13 @@ class API(web.View):
nsfw_prob = classify(image)
text = nsfw_prob.astype(str)
return web.Response(text=text)
- except:
- raise web.HTTPBadRequest()
+ except KeyError:
+ return HTTPBadRequest(text="Missing `url` POST parameter")
+ except OSError as e:
+ if "cannot identify" in str(e):
+ raise HTTPUnsupportedMediaType(text="Invalid image")
+ else:
+ raise e
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())