def exception_handler(environ: wsgi.WSGIEnviron, exc: Exception) -> http.Response: if isinstance(exc, exceptions.APIException): return http.Response({'message': exc.message}, exc.status_code) if is_running_from_reloader() or environ.get('APISTAR_RAISE_500_EXC'): raise message = traceback.format_exc() return http.Response(message, 500, {'Content-Type': 'text/plain; charset=utf-8'})
def exception_handler(environ: wsgi.WSGIEnviron, exc: Exception) -> http.Response: if isinstance(exc, exceptions.Found): return http.Response('', exc.status_code, {'Location': exc.location}) if isinstance(exc, exceptions.APIException): if isinstance(exc.detail, str): content = {'message': exc.detail} else: content = exc.detail return http.Response(content, exc.status_code) if is_running_from_reloader() or environ.get('APISTAR_RAISE_500_EXC'): raise message = traceback.format_exc() return http.Response(message, 500, {'Content-Type': 'text/plain; charset=utf-8'})
def serve_static(path: Path, statics: Statics, environ: wsgi.WSGIEnviron) -> wsgi.WSGIResponse: if not path.startswith('/'): path = Path('/' + path) static_file = statics.whitenoise.files.get(path) if static_file is None: raise exceptions.NotFound() response = static_file.get_response(environ['REQUEST_METHOD'], environ) status_line = '{} {}'.format(response.status, response.status.phrase) headers = list(response.headers) if response.file is not None: file_wrapper = environ.get('wsgi.file_wrapper', FileWrapper) content = file_wrapper(response.file) else: # We hit this branch for HEAD requests content = [] return wsgi.WSGIResponse(status_line, headers, content)