Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix custom exceptions #425

Merged
merged 4 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/litserve/loops/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import asyncio
import inspect
import logging
import pickle
import signal
import sys
import time
Expand Down Expand Up @@ -248,6 +249,7 @@ def populate_context(self, lit_spec: LitSpec, request: Any):
def put_response(
self, response_queues: List[Queue], response_queue_id: int, uid: str, response_data: Any, status: LitAPIStatus
) -> None:
response_data = pickle.dumps(response_data)
if self.producer:
self.producer.put((uid, (response_data, status)), consumer_id=response_queue_id)
else:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,21 @@ def test_concurrent_requests(lit_server):
assert response.json() == {"output": i**2}, "Server returns square of the input number"
count += 1
assert count == n_requests


class CustomError(Exception):
def __init__(self, arg1, arg2, arg3):
super().__init__("Test exception")


class ExceptionAPI(SimpleLitAPI):
def predict(self, x):
raise CustomError("This", "is", "a test")


def test_exception():
server = LitServer(ExceptionAPI(), accelerator="cpu", devices=1)
with wrap_litserve_start(server) as server, TestClient(server.app) as client:
response = client.post("/predict", json={"input": 4.0})
assert response.status_code == 500
assert response.json() == {"detail": "Internal Server Error"}
Loading