|
10 | 10 | from typing import TYPE_CHECKING, Any, Dict, Optional, cast
|
11 | 11 |
|
12 | 12 | from jupyter_core.utils import ensure_async
|
13 |
| -from jupyter_events.logger import SchemaNotRegistered |
14 | 13 | from tornado import web, websocket
|
15 | 14 |
|
16 | 15 | from jupyter_server.auth.decorator import authorized, ws_authenticated
|
@@ -72,12 +71,19 @@ def on_close(self):
|
72 | 71 | self.event_logger.remove_listener(listener=self.event_listener)
|
73 | 72 |
|
74 | 73 |
|
75 |
| -def validate_model(data: dict[str, Any]) -> None: |
76 |
| - """Validates for required fields in the JSON request body""" |
| 74 | +def validate_model(data: dict[str, Any], schema: jupyter_events.schema.EventSchema) -> None: |
| 75 | + """Validates for required fields in the JSON request body and verifies that |
| 76 | + a registered schema/version exists""" |
77 | 77 | required_keys = {"schema_id", "version", "data"}
|
78 | 78 | for key in required_keys:
|
79 | 79 | if key not in data:
|
80 |
| - raise web.HTTPError(400, f"Missing `{key}` in the JSON request body.") |
| 80 | + raise Exception(f"Missing `{key}` in the JSON request body.") |
| 81 | + schema_id = cast(str, data.get("schema_id")) |
| 82 | + version = cast(int, data.get("version")) |
| 83 | + if schema is None: |
| 84 | + raise Exception(f"Unregistered schema: `{schema_id}`") |
| 85 | + if schema.version != version: |
| 86 | + raise Exception(f"Unregistered version: `{version}` for `{schema_id}`") |
81 | 87 |
|
82 | 88 |
|
83 | 89 | def get_timestamp(data: dict[str, Any]) -> Optional[datetime]:
|
@@ -112,21 +118,19 @@ async def post(self):
|
112 | 118 | raise web.HTTPError(400, "No JSON data provided")
|
113 | 119 |
|
114 | 120 | try:
|
115 |
| - validate_model(payload) |
| 121 | + schema = self.event_logger.schemas.get(cast(str, payload.get("schema_id"))) |
| 122 | + validate_model(payload, schema) |
116 | 123 | self.event_logger.emit(
|
117 |
| - schema_id=cast(str, payload.get("schema_id")), |
| 124 | + schema_id=schema.id, |
118 | 125 | data=cast("Dict[str, Any]", payload.get("data")),
|
119 | 126 | timestamp_override=get_timestamp(payload),
|
120 | 127 | )
|
121 | 128 | self.set_status(204)
|
122 | 129 | self.finish()
|
123 |
| - except web.HTTPError: |
124 |
| - raise |
125 |
| - except SchemaNotRegistered as e: |
126 |
| - message = f"Unregistered event schema: ${str(e)}" |
127 |
| - raise web.HTTPError(400, message) from e |
128 | 130 | except Exception as e:
|
129 |
| - raise web.HTTPError(500, str(e)) from e |
| 131 | + # All known exceptions are raised by bad requests, e.g., bad |
| 132 | + # version, unregistered schema, invalid emission data payload, etc. |
| 133 | + raise web.HTTPError(400, str(e)) from e |
130 | 134 |
|
131 | 135 |
|
132 | 136 | default_handlers = [
|
|
0 commit comments