Skip to content

Commit

Permalink
doc: Custom Error Handling (#4811)
Browse files Browse the repository at this point in the history
  • Loading branch information
jianshen92 authored Jun 18, 2024
1 parent 08a15f8 commit b08848b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
38 changes: 38 additions & 0 deletions docs/source/guides/error-handling.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
=============
Error Handling
=============
BentoML allows user to define custom error handling logic for their Service.

This document describes how to define a custom exception class and error handling logic in a BentoML Service.

Custom Exception Class
-----------------------
User can define custom exception class by inheriting from `BentoMLException` or any of its subclasses, such as `InvalidArgument` in the example below.

.. note::

Error Code 401, 403, and >500 is reserved by BentoML, user should not use these error codes in their custom exception class.

.. code-block:: python
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")
8 changes: 7 additions & 1 deletion docs/source/guides/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,13 @@ This chapter introduces the key features of BentoML. We recommend you read :doc:
:link: /guides/lifecycle-hooks
:link-type: doc

Confgiure hooks to run custom logic at different stages of a Service's lifecycle.
Configure hooks to run custom logic at different stages of a Service's lifecycle.

.. grid-item-card:: :doc:`/guides/error-handling`
:link: /guides/error-handling
:link-type: doc

Define Custom Error Handling Logic for your BentoML Service.

.. toctree::
:hidden:
Expand Down

0 comments on commit b08848b

Please sign in to comment.