mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-26 19:24:27 +00:00
refactor PrismaDBExceptionHandler
This commit is contained in:
parent
0155b0eba2
commit
7142b0b610
2 changed files with 41 additions and 34 deletions
|
@ -9,13 +9,9 @@ from fastapi import HTTPException, Request, status
|
||||||
|
|
||||||
import litellm
|
import litellm
|
||||||
from litellm._logging import verbose_proxy_logger
|
from litellm._logging import verbose_proxy_logger
|
||||||
from litellm.proxy._types import (
|
from litellm.proxy._types import ProxyErrorTypes, ProxyException, UserAPIKeyAuth
|
||||||
DB_CONNECTION_ERROR_TYPES,
|
|
||||||
ProxyErrorTypes,
|
|
||||||
ProxyException,
|
|
||||||
UserAPIKeyAuth,
|
|
||||||
)
|
|
||||||
from litellm.proxy.auth.auth_utils import _get_request_ip_address
|
from litellm.proxy.auth.auth_utils import _get_request_ip_address
|
||||||
|
from litellm.proxy.db.exception_handler import PrismaDBExceptionHandler
|
||||||
from litellm.types.services import ServiceTypes
|
from litellm.types.services import ServiceTypes
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
|
@ -58,8 +54,8 @@ class UserAPIKeyAuthExceptionHandler:
|
||||||
)
|
)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
UserAPIKeyAuthExceptionHandler.should_allow_request_on_db_unavailable()
|
PrismaDBExceptionHandler.should_allow_request_on_db_unavailable()
|
||||||
and UserAPIKeyAuthExceptionHandler.is_database_connection_error(e)
|
and PrismaDBExceptionHandler.is_database_connection_error(e)
|
||||||
):
|
):
|
||||||
# log this as a DB failure on prometheus
|
# log this as a DB failure on prometheus
|
||||||
proxy_logging_obj.service_logging_obj.service_failure_hook(
|
proxy_logging_obj.service_logging_obj.service_failure_hook(
|
||||||
|
@ -125,29 +121,3 @@ class UserAPIKeyAuthExceptionHandler:
|
||||||
param=getattr(e, "param", "None"),
|
param=getattr(e, "param", "None"),
|
||||||
code=status.HTTP_401_UNAUTHORIZED,
|
code=status.HTTP_401_UNAUTHORIZED,
|
||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def should_allow_request_on_db_unavailable() -> bool:
|
|
||||||
"""
|
|
||||||
Returns True if the request should be allowed to proceed despite the DB connection error
|
|
||||||
"""
|
|
||||||
from litellm.proxy.proxy_server import general_settings
|
|
||||||
|
|
||||||
if general_settings.get("allow_requests_on_db_unavailable", False) is True:
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def is_database_connection_error(e: Exception) -> bool:
|
|
||||||
"""
|
|
||||||
Returns True if the exception is from a database outage / connection error
|
|
||||||
"""
|
|
||||||
import prisma
|
|
||||||
|
|
||||||
if isinstance(e, DB_CONNECTION_ERROR_TYPES):
|
|
||||||
return True
|
|
||||||
if isinstance(e, prisma.errors.PrismaError):
|
|
||||||
return True
|
|
||||||
if isinstance(e, ProxyException) and e.type == ProxyErrorTypes.no_db_connection:
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
37
litellm/proxy/db/exception_handler.py
Normal file
37
litellm/proxy/db/exception_handler.py
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
from litellm.proxy._types import (
|
||||||
|
DB_CONNECTION_ERROR_TYPES,
|
||||||
|
ProxyErrorTypes,
|
||||||
|
ProxyException,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class PrismaDBExceptionHandler:
|
||||||
|
"""
|
||||||
|
Class to handle DB Exceptions or Connection Errors
|
||||||
|
"""
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def should_allow_request_on_db_unavailable() -> bool:
|
||||||
|
"""
|
||||||
|
Returns True if the request should be allowed to proceed despite the DB connection error
|
||||||
|
"""
|
||||||
|
from litellm.proxy.proxy_server import general_settings
|
||||||
|
|
||||||
|
if general_settings.get("allow_requests_on_db_unavailable", False) is True:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def is_database_connection_error(e: Exception) -> bool:
|
||||||
|
"""
|
||||||
|
Returns True if the exception is from a database outage / connection error
|
||||||
|
"""
|
||||||
|
import prisma
|
||||||
|
|
||||||
|
if isinstance(e, DB_CONNECTION_ERROR_TYPES):
|
||||||
|
return True
|
||||||
|
if isinstance(e, prisma.errors.PrismaError):
|
||||||
|
return True
|
||||||
|
if isinstance(e, ProxyException) and e.type == ProxyErrorTypes.no_db_connection:
|
||||||
|
return True
|
||||||
|
return False
|
Loading…
Add table
Add a link
Reference in a new issue