Skip to content

Commit

Permalink
Update exception strategy and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
afshin committed Oct 29, 2024
1 parent f5bb5a8 commit 8c298d7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
15 changes: 7 additions & 8 deletions jupyter_server/services/events/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from typing import TYPE_CHECKING, Any, Dict, Optional, cast

from jupyter_core.utils import ensure_async
from jupyter_events.logger import SchemaNotRegistered
from tornado import web, websocket

from jupyter_server.auth.decorator import authorized, ws_authenticated
Expand Down Expand Up @@ -112,21 +111,21 @@ async def post(self):
raise web.HTTPError(400, "No JSON data provided")

try:
# Validate payload and verify schema is registered.
validate_model(payload)
schema_id = cast(str, payload.get("schema_id"))
schema = self.event_logger.schemas.get(schema_id)
self.event_logger.emit(
schema_id=cast(str, payload.get("schema_id")),
schema_id=schema.id,
data=cast("Dict[str, Any]", payload.get("data")),
timestamp_override=get_timestamp(payload),
)
self.set_status(204)
self.finish()
except web.HTTPError:
raise
except SchemaNotRegistered as e:
message = f"Unregistered event schema: ${str(e)}"
raise web.HTTPError(400, message) from e
except Exception as e:
raise web.HTTPError(500, str(e)) from e
# All known exceptions are raised by bad requests, e.g.,
# unregistered schema, invalid emission data payload, etc.
raise web.HTTPError(400, str(e)) from e


default_handlers = [
Expand Down
24 changes: 10 additions & 14 deletions tests/services/events/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,6 @@ async def test_post_event(jp_fetch, event_logger_sink, payload):
}
"""


@pytest.mark.parametrize("payload", [payload_3, payload_4, payload_5, payload_6, payload_7])
async def test_post_event_400(jp_fetch, event_logger, payload):
with pytest.raises(tornado.httpclient.HTTPClientError) as e:
await jp_fetch("api", "events", method="POST", body=payload)

assert expected_http_error(e, 400)


payload_8 = """\
{
"schema_id": "http://event.mock.jupyter.org/message",
Expand All @@ -157,9 +148,14 @@ async def test_post_event_400(jp_fetch, event_logger, payload):
"""


@pytest.mark.parametrize("payload", [payload_8, payload_9])
async def test_post_event_500(jp_fetch, event_logger, payload):
with pytest.raises(tornado.httpclient.HTTPClientError) as e:
@pytest.mark.parametrize(
"payload",
[payload_3, payload_4, payload_5, payload_6, payload_7, payload_8, payload_9],
)
async def test_post_event_400(jp_fetch, event_logger, payload):
try:
await jp_fetch("api", "events", method="POST", body=payload)

assert expected_http_error(e, 500)
except Exception as exception:
assert exception.code == 400
return
assert "failed" == True

0 comments on commit 8c298d7

Please sign in to comment.