seldon-core icon indicating copy to clipboard operation
seldon-core copied to clipboard

[Feature Request] Custom Exception Handling for GRPC endpoints

Open SachinVarghese opened this issue 4 years ago • 1 comments

Based on the docs, seldon core supports custom error handling for REST endpoints. As an example,

"""
Model Template
"""
class MyModel(Object):

    """
    The field is used to register custom exceptions
    """
    model_error_handler = flask.Blueprint('error_handlers', __name__)

    """
    Register the handler for an exception
    """
    @model_error_handler.app_errorhandler(UserCustomException)
    def handleCustomError(error):
        response = jsonify(error.to_dict())
        response.status_code = error.status_code
        return response

    def __init__(self, metrics_ok=True, ret_nparray=False, ret_meta=False):
        pass

    def predict(self, X, features_names, **kwargs):
        raise UserCustomException('Test-Error-Msg',1402,402)
        return X
"""
User Defined Exception
"""
class UserCustomException(Exception):

    status_code = 404

    def __init__(self, message, application_error_code,http_status_code):
        Exception.__init__(self)
        self.message = message
        if http_status_code is not None:
            self.status_code = http_status_code
        self.application_error_code = application_error_code

    def to_dict(self):
        rv = {"status": {"status": self.status_code, "message": self.message,
                         "app_code": self.application_error_code}}
        return rv

The main implementation detail is that for the REST server is uses the underlying flask app to handle error as per the following source lines of code

if hasattr(user_model, "model_error_handler"):
        logger.info("Registering the custom error handler...")
        app.register_blueprint(user_model.model_error_handler)

Since the error is based on the SeldonMessage proto message Status, a similar server level impl can be built for handling custom errors in the GRPC server across model level methods like predictions and feedback.

This would also involve updatingthe tests to include GRPC exception handling along with current tests for REST endpoints

SachinVarghese avatar Oct 07 '21 19:10 SachinVarghese

I did some investigation on this and I can't find a quick solution yet. Basically in REST servers we use Flask blueprints for registering custom errors handling. If we don't want to change that behaviour in GRPC, we would need to implement a flask.register_blueprint functionality for GRPC servers and handle errors gracefully which I don't think is straightforward.

There could be other solutions with just GRPC context such that the user handles the error correctly and sends back a SeldonMessage with custom errors in status, but then that is deviating from the REST implementation.

SachinVarghese avatar Feb 14 '22 16:02 SachinVarghese

Try in MLServer

ukclivecox avatar Dec 19 '22 11:12 ukclivecox