mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-10-01 16:18:11 +08:00
REST Error handling (#299)
* Use HTTPStatus for all responses More informative than just the code as an int * Fill out REST error handling * Test error routes * Use HTTP Status for tests too * factor mime type request into function * Better mimetype errors * 404 -> 400 error for bad key
This commit is contained in:
@@ -10,3 +10,24 @@ class Float32JSONEncoder(json.JSONEncoder):
|
||||
elif isinstance(obj, integer):
|
||||
return int(obj)
|
||||
return json.JSONEncoder.default(self, obj)
|
||||
|
||||
|
||||
class MimeTypeError(Exception):
|
||||
|
||||
def __init__(self, message):
|
||||
self.message = message
|
||||
|
||||
|
||||
def get_mime_type(default="application/json", acceptable_types=["application/json", "text/csv"], query_param=None,
|
||||
header=None):
|
||||
mime_type = default
|
||||
if query_param:
|
||||
if query_param in acceptable_types:
|
||||
mime_type = query_param
|
||||
else:
|
||||
raise MimeTypeError(f"Unsupported mime type {query_param} specified in query parameter 'accept-type'")
|
||||
elif len(header):
|
||||
mime_type = header.best_match(acceptable_types)
|
||||
if not mime_type:
|
||||
raise MimeTypeError(f"Unsupported mime type(s) {header} in HTTP Accept header")
|
||||
return mime_type
|
||||
|
||||
Reference in New Issue
Block a user