Kevin Cen
05/28/2024, 2:15 PMTim Liu
05/28/2024, 3:58 PMKevin Cen
05/28/2024, 7:52 PMJian Shen Yap
05/28/2024, 8:54 PMKevin Cen
05/28/2024, 8:56 PMChaoyu
05/29/2024, 8:10 AMBentoMLException
2. Define the error_code class attribute for your exception. Only code <500 and not 401, 403 are considered user facing errors and are returned as response
3. Raise exception in your service API function where it fits
Here’s an example:
import bentoml
from bentoml.exceptions import InvalidArgument, BentoMLException
from http import HTTPStatus
class MyCustomException(BentoMLException):
error_code = HTTPStatus.METHOD_NOT_ALLOWED
class MyCustomInvalidArgsException(InvalidArgument):
pass
@bentoml.service
class MyService:
@bentoml.api
def test1(self, text: str) -> str:
raise MyCustomException("this is a custom error message")
@bentoml.api
def test2(self, text: str) -> str:
raise MyCustomInvalidArgsException("this is a custom error message")
You can test this on the client side:
curl --header "Content-Type: application/json" \
--request POST \
--data '{"text":"abc"}' \
<http://localhost:3000/test1>
[{"error":"this is a custom error message"}]%Chaoyu
05/29/2024, 8:10 AMChaoyu
05/29/2024, 8:10 AMJian Shen Yap
05/29/2024, 9:28 AMKevin Cen
05/29/2024, 1:07 PM