You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
941 B
Python
28 lines
941 B
Python
import sys
|
|
|
|
from apistar import App, exceptions
|
|
from apistar.http import Response, HTMLResponse
|
|
from apistar.server.components import ReturnValue
|
|
from .http import MetaJSONResponse
|
|
|
|
|
|
class MetaApp(App):
|
|
"""
|
|
A WSGI App subclass with a MetaJSONResponse default response type
|
|
"""
|
|
|
|
def render_response(self, return_value: ReturnValue) -> Response:
|
|
if isinstance(return_value, Response):
|
|
return return_value
|
|
elif isinstance(return_value, str):
|
|
return HTMLResponse(return_value)
|
|
return MetaJSONResponse(return_value)
|
|
|
|
def exception_handler(self, exc: Exception) -> Response:
|
|
if isinstance(exc, exceptions.HTTPException):
|
|
|
|
return MetaJSONResponse(exc.detail, status_code=exc.status_code, headers=exc.get_headers())
|
|
raise
|
|
|
|
def error_handler(self) -> Response:
|
|
return MetaJSONResponse('Server error', status_code=500, exc_info=sys.exc_info()) |