Ejemplo n.º 1
0
        def api_func():
            # handle_request may raise 4xx or 5xx exception.
            try:
                if request.headers.get(MARSHAL_REQUEST_HEADER):
                    reqs = DataLoader.split_requests(request.get_data())
                    responses = api.handle_batch_request(reqs)
                    response_body = DataLoader.merge_responses(responses)
                    response = make_response(response_body)
                else:
                    response = api.handle_request(request)
            except BentoMLException as e:
                log_exception(sys.exc_info())

                if 400 <= e.status_code < 500 and e.status_code not in (401,
                                                                        403):
                    response = make_response(
                        jsonify(message=
                                "BentoService error handling API request: %s" %
                                str(e)),
                        e.status_code,
                    )
                else:
                    response = make_response('', e.status_code)
            except Exception:  # pylint: disable=broad-except
                # For all unexpected error, return 500 by default. For example,
                # if users' model raises an error of division by zero.
                log_exception(sys.exc_info())

                response = make_response(
                    'An error has occurred in BentoML user code when handling this '
                    'request, find the error details in server logs',
                    500,
                )

            return response
Ejemplo n.º 2
0
 def handle_batch_request(self, request):
     requests = DataLoader.split_requests(request.data)
     with trace(
         ZIPKIN_API_URL,
         service_name=self.__class__.__name__,
         span_name=f"call `{self._handler.__class__.__name__}`",
     ):
         responses = self.handler.handle_batch_request(requests, self.func)
     return DataLoader.merge_responses(responses)
Ejemplo n.º 3
0
    def handle_batch_request(self, request: flask.Request):
        from bentoml.marshal.utils import DataLoader

        requests = DataLoader.split_requests(request.get_data())

        with trace(
                service_name=self.__class__.__name__,
                span_name=f"call `{self.handler.__class__.__name__}`",
        ):
            responses = self.handler.handle_batch_request(requests, self.func)

        return DataLoader.merge_responses(responses)