Hello, I am using BentoML version 1.1.11. I would ...
# ask-for-help
k
Hello, I am using BentoML version 1.1.11. I would like the ability to customize the error message for the BentoML API service. The default is "An error has occurred in BentoML user code when handling this request, find the error details in server logs" and I would like to change the message returned based on the error that happened. Is this possible to do? The only solution I have come up with is to define an endpoint in FastAPI and mount it onto the BentoML service API.
t
Hi @Kevin Cen! We've had this request a few times recently. Will add this to the product backlog. I think it makes sense
👍 2
k
@Tim Liu Thank you for the reply! Would implementing a middleware to handle error messages be a possible workaround solution or would BentoML internally still return the same error message? I have tried but have not been able to get it to work.
j
Have you tried using BentoMLException class to define your error messages
k
I have, but I had the same issue with the API returning the same error message.
c
@Kevin Cen this is already supported! 1. Create a custom exception class inherited from
BentoMLException
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:
Copy code
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:
Copy code
curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"text":"abc"}' \
  <http://localhost:3000/test1>
[{"error":"this is a custom error message"}]%
👍 1
@Jian Shen Yap could you help with documenting this?
👍 1
j
i think there's a chance where the custom exception is not working properly in 1.11 but in the latest 1.2 it was fixed. will take a look !
k
@Chaoyu Ah, I got it to work now. I was using the BentoMLException with an error code of 500 so it was not working before. Thank you!
👍 1