Skip to content

Commit

Permalink
Implementation of #625
Browse files Browse the repository at this point in the history
  • Loading branch information
lkuma37 committed Jun 25, 2019
1 parent c5d7b30 commit 252bbb8
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
4 changes: 4 additions & 0 deletions python/seldon_core/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def get_rest_microservice(user_model):
app = Flask(__name__, static_url_path='')
CORS(app)

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

@app.errorhandler(SeldonMicroserviceException)
def handle_invalid_usage(error):
response = jsonify(error.to_dict())
Expand Down
93 changes: 93 additions & 0 deletions python/tests/test_application_exception_microservice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import json
import numpy as np
from google.protobuf import json_format
import base64
import tensorflow as tf
from tensorflow.core.framework.tensor_pb2 import TensorProto

from seldon_core.wrapper import get_rest_microservice, SeldonModelGRPC, get_grpc_server
from seldon_core.proto import prediction_pb2
from seldon_core.user_model import SeldonComponent

import flask
from flask import jsonify

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

class UserObject(SeldonComponent):

model_error_handler = flask.Blueprint('error_handlers', __name__)

@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):
self.metrics_ok = metrics_ok
self.ret_nparray = ret_nparray
self.nparray = np.array([1, 2, 3])
self.ret_meta = ret_meta

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


class UserObjectLowLevel(SeldonComponent):

model_error_handler = flask.Blueprint('error_handlers', __name__)

@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):
self.metrics_ok = metrics_ok
self.ret_nparray = ret_nparray
self.nparray = np.array([1, 2, 3])

def predict_rest(self, request):
raise UserCustomException('Test-Error-Msg',1402,402)
return {"data": {"ndarray": [9, 9]}}


def test_raise_exception():
user_object = UserObject()
app = get_rest_microservice(user_object)
client = app.test_client()
rv = client.get('/predict?json={"data":{"names":["a","b"],"ndarray":[[1,2]]}}')
j = json.loads(rv.data)
print(j)
assert rv.status_code == 402
assert j["status"]["app_code"] == 1402


def test_raise_eception_lowlevel():
user_object = UserObjectLowLevel()
app = get_rest_microservice(user_object)
client = app.test_client()
rv = client.get('/predict?json={"data":{"ndarray":[1,2]}}')
j = json.loads(rv.data)
print(j)
assert rv.status_code == 402
assert j["status"]["app_code"] == 1402

0 comments on commit 252bbb8

Please sign in to comment.