Render the exception classes for the BiDi ``ErrorCode`` enum. Emitted as real class statements rather than synthesized at import, so the classes type-check, autocomplete and document like every other exception in the bindings.
(schema: Schema)
| 1190 | |
| 1191 | |
| 1192 | def render_errors(schema: Schema) -> str: |
| 1193 | """Render the exception classes for the BiDi ``ErrorCode`` enum. |
| 1194 | |
| 1195 | Emitted as real class statements rather than synthesized at import, so the classes |
| 1196 | type-check, autocomplete and document like every other exception in the bindings. |
| 1197 | """ |
| 1198 | classic = _classic_exceptions() |
| 1199 | resolved = {code: classic.get(code) for code in schema.types["ErrorCode"]["values"]} |
| 1200 | |
| 1201 | imported = sorted({exc.__name__ for exc in resolved.values() if exc is not None} | {"WebDriverException"}) |
| 1202 | import_block = "\n".join(["from selenium.common.exceptions import (", *(f" {n}," for n in imported), ")"]) |
| 1203 | |
| 1204 | declarations = [ |
| 1205 | f'class {exception_class_name(code)}(WebDriverException):\n """Raised for the BiDi {lit(code)} error."""' |
| 1206 | for code, exc in resolved.items() |
| 1207 | if exc is None |
| 1208 | ] |
| 1209 | |
| 1210 | entries = [ |
| 1211 | f" {lit(code)}: {exc.__name__ if exc is not None else exception_class_name(code)}," |
| 1212 | for code, exc in resolved.items() |
| 1213 | ] |
| 1214 | table = "\n".join(["EXCEPTIONS: dict[str, type[WebDriverException]] = {", *entries, "}"]) |
| 1215 | |
| 1216 | lookup = '''def exception_for(code: str | None) -> type[WebDriverException]: |
| 1217 | """The exception class for a wire error code, falling back for an unrecognized one. |
| 1218 | |
| 1219 | An error the remote end reports must surface as that error even when the code is one |
| 1220 | this schema does not declare, so an unknown code is never a serialization failure. |
| 1221 | """ |
| 1222 | return EXCEPTIONS.get(code, WebDriverException) if code else WebDriverException''' |
| 1223 | |
| 1224 | blocks = [_HEADER, _ERRORS_DOCSTRING, "from __future__ import annotations", import_block] |
| 1225 | blocks += declarations |
| 1226 | blocks += [table, lookup] |
| 1227 | return "\n\n\n".join(blocks) + "\n" |
| 1228 | |
| 1229 | |
| 1230 | def render_all(schema_path: str) -> dict[str, str]: |
no test coverage detected