예제 #1
0
    def get_concrete_response(self):
        try:
            result = self.get_data_response()
        except NoViewMethod as exc:
            return HTTPUnsupportedMediaType(body=render_error_page(
                415, exc, mimetype=self.mimetype),
                                            content_type="text/html")
        except InvalidInput as exc:
            ## if the model raises a InvalidInput, retry the request as
            ## a GET request for the same program, and set the code to 400.
            request = make_duplicate_request(self.request)
            request.method = 'GET'
            c = HTTPController(request,
                               self.manifest,
                               self.model_mock,
                               errors=exc)
            response = c.get_response()
            response.status_int = 400
            return response
        except NotAuthorized as exc:
            return HTTPForbidden(body=render_error_page(
                403, exc, mimetype=self.mimetype),
                                 content_type="text/html")
        except (DataNotFound, ProgramNotFound) as exc:
            return HTTPNotFound(body=render_error_page(404,
                                                       exc,
                                                       mimetype=self.mimetype),
                                content_type="text/html")

        if type(result['body']) == Redirection:
            response = HTTPFound(location=result['body'].path)
        else:
            lazy = None
            body = result['body']

            if type(body) == tuple:
                lazy = body
                body = ''

            if hasattr(body, 'read'):
                body = body.read()

            response = Response(
                status=200,
                body=body,
                content_type=result['mimetype'],
            )

            response.lazy_data = lazy

        return response
예제 #2
0
    def get_concrete_response(self):
        try:
            result = self.get_data_response()
        except NoViewMethod as exc:
            return HTTPUnsupportedMediaType(
                body=render_error_page(415, exc),
                content_type="text/html"
            )
        except InvalidInput as exc:
            ## if the model raises a InvalidInput, retry the request as
            ## a GET request for the same program, and set the code to 400.
            request = make_duplicate_request(self.request)
            request.method = 'GET'
            c = HTTPController(request, self.manifest, self.model_mock, errors=exc)
            response = c.get_response()
            response.status_int = 400
            return response
        except NotAuthorized as exc:
            return HTTPForbidden(
                body=render_error_page(403, exc),
                content_type="text/html"
            )
        except (DataNotFound, ProgramNotFound) as exc:
            return HTTPNotFound(
                body=render_error_page(404, exc),
                content_type="text/html"
            )

        if type(result['body']) == Redirection:
            response = HTTPTemporaryRedirect(location=result['body'].path)
        else:
            lazy = None
            body = result['body']

            if type(body) == tuple:
                lazy = body
                body = ''

            if hasattr(body, 'read'):
                body = body.read()

            response = Response(
                status=200,
                body=body,
                content_type=result['mimetype'],
            )

            response.lazy_data = lazy

        return response
예제 #3
0
 def application(environ, start_response):
     try:
         return app(environ, start_response)
     except Exception as exc:
         sio = StringIO()
         traceback.print_exc(file=sio)
         sio.seek(0)
         response = Response(status=500,
                             body=render_error_page(500,
                                                    exc,
                                                    traceback=sio.read()),
                             content_type="text/html")
         return response(environ, start_response)
예제 #4
0
 def application(environ, start_response):
     try:
         return app(environ, start_response)
     except Exception as exc:
         sio = StringIO()
         traceback.print_exc(file=sio)
         sio.seek(0)
         response =  Response(
             status=500,
             body=render_error_page(500, exc, sio.read()),
             content_type="text/html"
         )
         return response(environ, start_response)