-
Notifications
You must be signed in to change notification settings - Fork 837
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #651 from klalit/master
Allow User Defined Exceptions
- Loading branch information
Showing
3 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|