|
18 | 18 | UsageData, |
19 | 19 | wrap_in_fastapi, |
20 | 20 | ) |
| 21 | +from unstructured_platform_plugins.generated.error_audience_v1 import ErrorAudience |
21 | 22 | from unstructured_platform_plugins.schema.filedata_meta import FileDataMeta |
22 | 23 |
|
23 | 24 |
|
| 25 | +class PluginErrorMetadata(BaseModel): |
| 26 | + error_type: str |
| 27 | + error_reason: str |
| 28 | + dependency: Optional[str] = None |
| 29 | + audience: Optional[ErrorAudience] = None |
| 30 | + retryable: bool = False |
| 31 | + |
| 32 | + |
24 | 33 | class InvokeResponse(BaseModel): |
25 | 34 | usage: list[UsageData] |
26 | 35 | status_code: int |
27 | 36 | filedata_meta: FileDataMeta |
28 | 37 | status_code_text: Optional[str] = None |
| 38 | + plugin_error: Optional[PluginErrorMetadata] = None |
29 | 39 | output: Optional[Any] = None |
30 | 40 | file_data: Optional[Union[FileData, BatchFileData]] = None |
31 | 41 |
|
@@ -222,6 +232,67 @@ def test_http_exception_handling(file_data): |
222 | 232 | assert invoke_response.status_code_text == "Not found" |
223 | 233 |
|
224 | 234 |
|
| 235 | +@pytest.mark.parametrize( |
| 236 | + "file_data", mock_file_data, ids=[type(fd).__name__ for fd in mock_file_data] |
| 237 | +) |
| 238 | +def test_user_error_declares_canonical_user_audience(file_data): |
| 239 | + """Only the UserError family declares a user-actionable plugin error.""" |
| 240 | + from test.assets.exception_status_code import function_raises_user_error as test_fn |
| 241 | + |
| 242 | + client = TestClient(wrap_in_fastapi(func=test_fn, plugin_id="mock_plugin")) |
| 243 | + |
| 244 | + resp = client.post("/invoke", json={"file_data": file_data.model_dump()}) |
| 245 | + invoke_response = InvokeResponse.model_validate(resp.json()) |
| 246 | + |
| 247 | + assert invoke_response.status_code >= 400 |
| 248 | + assert invoke_response.plugin_error is not None |
| 249 | + assert invoke_response.plugin_error.audience is ErrorAudience.USER |
| 250 | + assert invoke_response.plugin_error.error_type == "configuration" |
| 251 | + assert invoke_response.plugin_error.error_reason == "invalid_input" |
| 252 | + assert invoke_response.plugin_error.retryable is False |
| 253 | + |
| 254 | + |
| 255 | +@pytest.mark.parametrize( |
| 256 | + "file_data", mock_file_data, ids=[type(fd).__name__ for fd in mock_file_data] |
| 257 | +) |
| 258 | +def test_non_user_failures_declare_no_plugin_error(file_data): |
| 259 | + """Anything undeclared is not the customer's: an orchestrator must not infer customer fault |
| 260 | + from the status code, which also carries transport semantics.""" |
| 261 | + from test.assets.exception_status_code import function_raises_provider_error as test_fn |
| 262 | + |
| 263 | + client = TestClient(wrap_in_fastapi(func=test_fn, plugin_id="mock_plugin")) |
| 264 | + |
| 265 | + resp = client.post("/invoke", json={"file_data": file_data.model_dump()}) |
| 266 | + invoke_response = InvokeResponse.model_validate(resp.json()) |
| 267 | + |
| 268 | + assert invoke_response.plugin_error is None |
| 269 | + |
| 270 | + |
| 271 | +def test_streaming_user_error_declares_user_audience(): |
| 272 | + """The streaming error envelope carries the same audience as the non-streaming path.""" |
| 273 | + from test.assets.exception_status_code import ( |
| 274 | + async_gen_function_raises_user_error_mid_stream as test_fn, |
| 275 | + ) |
| 276 | + |
| 277 | + client = TestClient(wrap_in_fastapi(func=test_fn, plugin_id="mock_plugin")) |
| 278 | + |
| 279 | + resp = client.post("/invoke", json={"file_data": mock_file_data[0].model_dump()}) |
| 280 | + |
| 281 | + assert resp.status_code == 200 |
| 282 | + assert resp.headers["content-type"] == "application/x-ndjson" |
| 283 | + |
| 284 | + import json |
| 285 | + |
| 286 | + lines = resp.content.decode().strip().split("\n") |
| 287 | + assert len(lines) == 2 # One yielded item, then the error envelope |
| 288 | + |
| 289 | + assert InvokeResponse.model_validate(json.loads(lines[0])).plugin_error is None |
| 290 | + error_response = InvokeResponse.model_validate(json.loads(lines[1])) |
| 291 | + assert error_response.status_code >= 400 |
| 292 | + assert error_response.plugin_error is not None |
| 293 | + assert error_response.plugin_error.audience is ErrorAudience.USER |
| 294 | + |
| 295 | + |
225 | 296 | @pytest.mark.parametrize( |
226 | 297 | "file_data", mock_file_data, ids=[type(fd).__name__ for fd in mock_file_data] |
227 | 298 | ) |
@@ -608,9 +679,26 @@ def test_precheck_reports_failure_category_from_raised_error(): |
608 | 679 | body = resp.json() |
609 | 680 | assert body["status_code"] == 403 |
610 | 681 | assert body["failure_category"] == "AUTH_PERMISSION_DENIED" |
| 682 | + # A plain exception is not the customer's to fix. |
| 683 | + assert body["plugin_error"] is None |
611 | 684 | assert "credential rejected" in body["status_code_text"] |
612 | 685 |
|
613 | 686 |
|
| 687 | +def test_precheck_declares_user_audience_like_invoke_does(): |
| 688 | + from unstructured_ingest.error import UserError |
| 689 | + |
| 690 | + def user_fault_precheck() -> None: |
| 691 | + raise UserError("bad credentials") |
| 692 | + |
| 693 | + client = TestClient( |
| 694 | + wrap_in_fastapi(func=_no_params, plugin_id="mock_plugin", precheck_func=user_fault_precheck) |
| 695 | + ) |
| 696 | + |
| 697 | + body = client.get("/precheck").json() |
| 698 | + |
| 699 | + assert body["plugin_error"]["audience"] == "user" |
| 700 | + |
| 701 | + |
614 | 702 | def test_precheck_success_has_no_failure_category(): |
615 | 703 | client = TestClient( |
616 | 704 | wrap_in_fastapi(func=_no_params, plugin_id="mock_plugin", precheck_func=_passing_precheck) |
|
0 commit comments