aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRahiel Kasim <rahielkasim@gmail.com>2018-01-22 15:37:11 +0100
committerRahiel Kasim <rahielkasim@gmail.com>2018-01-22 15:37:11 +0100
commit52c4aea5b5ca43fb8fa16fb5c446c40cf6711b1e (patch)
tree77eb47c153aa6ac248a0f6c0b4161f013127e355
parentcd53495a2b3f29bcdcbf7953bc1b8bc3479bfa64 (diff)
handle 404 errors on image URLs
-rw-r--r--README.md12
-rw-r--r--api.py4
2 files changed, 11 insertions, 5 deletions
diff --git a/README.md b/README.md
index 1f15e17..fb005fe 100644
--- a/README.md
+++ b/README.md
@@ -52,10 +52,14 @@ 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.
+
+## 404 Not Found
+
+The requested `url` leads to an HTTP 404 Not Found error.
+
+## 415 Unsupported Media Type: Invalid image
+
+The requested `url` is not a valid image.
diff --git a/api.py b/api.py
index 39a4937..588ebdb 100644
--- a/api.py
+++ b/api.py
@@ -5,7 +5,7 @@ import async_timeout
import numpy as np
import uvloop
from aiohttp import web
-from aiohttp.web import HTTPBadRequest, HTTPUnsupportedMediaType
+from aiohttp.web import HTTPBadRequest, HTTPNotFound, HTTPUnsupportedMediaType
from classify_nsfw import caffe_preprocess_and_compute, load_model
@@ -20,6 +20,8 @@ def classify(image: bytes) -> np.float64:
async def fetch(session, url):
with async_timeout.timeout(10):
async with session.get(url) as response:
+ if response.status == 404:
+ raise HTTPNotFound()
return await response.read()
class API(web.View):