diff --git a/jupyter_server/services/events/handlers.py b/jupyter_server/services/events/handlers.py index a345df27b..0357a9f45 100644 --- a/jupyter_server/services/events/handlers.py +++ b/jupyter_server/services/events/handlers.py @@ -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 @@ -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 = [ diff --git a/tests/services/events/test_api.py b/tests/services/events/test_api.py index 019a44e21..6dfebd0ba 100644 --- a/tests/services/events/test_api.py +++ b/tests/services/events/test_api.py @@ -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", @@ -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