|
| 1 | +"""Simulate the validation-error log path without hitting the server. |
| 2 | +
|
| 3 | +Goal: show what an operator would see in mgr logs when |
| 4 | +``BackendAISchema.model_validate`` fails and the result propagates |
| 5 | +through ``convert_validation_error`` -> exception middleware. |
| 6 | +
|
| 7 | +Run:: |
| 8 | +
|
| 9 | + PY=dist/export/python/virtualenvs/python-default/3.13.7/bin/python |
| 10 | + PYTHONPATH=src $PY scripts/show_validation_log_format.py |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import logging |
| 16 | + |
| 17 | +from ai.backend.common.api_handlers import convert_validation_error |
| 18 | +from ai.backend.common.exception import ( |
| 19 | + BackendAIError, |
| 20 | + BackendAISchemaValidationFailed, |
| 21 | + InvalidAPIParameters, |
| 22 | +) |
| 23 | +from ai.backend.common.types import BackendAISchema |
| 24 | + |
| 25 | +logging.basicConfig( |
| 26 | + level=logging.WARNING, |
| 27 | + format="%(asctime)s %(levelname)s %(name)s [%(process)d] %(message)s", |
| 28 | +) |
| 29 | +log = logging.getLogger("ai.backend.manager.api.rest.middleware.exception") |
| 30 | + |
| 31 | + |
| 32 | +class Address(BackendAISchema): |
| 33 | + city: str |
| 34 | + zipcode: int |
| 35 | + |
| 36 | + |
| 37 | +class User(BackendAISchema): |
| 38 | + name: str |
| 39 | + age: int |
| 40 | + address: Address |
| 41 | + |
| 42 | + |
| 43 | +@convert_validation_error |
| 44 | +def parse_body(model: type[BackendAISchema], body: dict) -> BackendAISchema: |
| 45 | + """Stand-in for ``BodyParam.from_body`` — same decorator wrapping.""" |
| 46 | + return model.model_validate(body) |
| 47 | + |
| 48 | + |
| 49 | +def middleware_log(ex: BackendAIError, *, method: str, endpoint: str) -> None: |
| 50 | + """Same call shape the real exception middleware uses.""" |
| 51 | + log.warning( |
| 52 | + "client error raised inside handlers: (%s %s): %s", |
| 53 | + method, |
| 54 | + endpoint, |
| 55 | + repr(ex), |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +def banner(title: str) -> None: |
| 60 | + print() |
| 61 | + print("=" * 78) |
| 62 | + print(title) |
| 63 | + print("=" * 78) |
| 64 | + |
| 65 | + |
| 66 | +# === Scenario 1: schema with multiple nested missing fields =============== |
| 67 | +banner("[1] body={} for User → caught by BackendAISchema.model_validate") |
| 68 | +try: |
| 69 | + User.model_validate({}) |
| 70 | +except BackendAISchemaValidationFailed as e: |
| 71 | + print('e!!', e) |
| 72 | + |
| 73 | + |
| 74 | +# === Scenario 2: same body going through convert_validation_error ========= |
| 75 | +banner("[2] body={} for User → caught by BodyParam (convert_validation_error)") |
| 76 | +try: |
| 77 | + parse_body(User, {}) |
| 78 | +except InvalidAPIParameters as e: |
| 79 | + print(f"-- type: {type(e).__name__} (← wrapped, not BackendAISchemaValidationFailed)") |
| 80 | + print(f"-- status_code: {e.status_code}") |
| 81 | + print(f"-- error_type: {e.error_type}") |
| 82 | + print(f"-- error_title: {e.error_title}") |
| 83 | + print(f"-- extra_msg (str):\n{e.extra_msg}") |
| 84 | + print(f"-- extra_data: {e.extra_data!r}") |
| 85 | + print() |
| 86 | + print("-- middleware log line would be:") |
| 87 | + middleware_log(e, method="POST", endpoint="/v2/users") |
| 88 | + |
| 89 | + |
| 90 | +# === Scenario 3: partial body with nested + sibling errors ================ |
| 91 | +banner("[3] body with mixed errors going through convert_validation_error") |
| 92 | +bad_body = { |
| 93 | + "name": "alice", |
| 94 | + "age": "not-an-int", |
| 95 | + "address": {"city": "Seoul"}, |
| 96 | +} |
| 97 | +try: |
| 98 | + parse_body(User, bad_body) |
| 99 | +except InvalidAPIParameters as e: |
| 100 | + print(f"-- type: {type(e).__name__}") |
| 101 | + print(f"-- extra_msg (str):\n{e.extra_msg}") |
| 102 | + print(f"-- extra_data: {e.extra_data!r}") |
| 103 | + print() |
| 104 | + print("-- middleware log line would be:") |
| 105 | + middleware_log(e, method="POST", endpoint="/v2/users") |
0 commit comments