mirror of
https://github.com/hansjone/oclaw.git
synced 2026-10-12 00:10:44 +08:00
初始化:独立 oclaw 仓库首提交
- 在 oclaw/ 下重新初始化 Git 仓库 - 补齐子仓库 .gitignore,避免提交本地运行态数据(_local、node_modules、logs 等) - 提交当前工程代码与配置 Made-with: Cursor
This commit is contained in:
commit
ba3836f00f
579 changed files with 83112 additions and 0 deletions
40
gateway/server_methods/validation.py
Normal file
40
gateway/server_methods/validation.py
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
|
||||
def error_shape(code: str, message: str, data: dict[str, Any] | None = None) -> dict[str, Any]:
|
||||
out: dict[str, Any] = {"code": str(code or "UNKNOWN"), "message": str(message or "")}
|
||||
if isinstance(data, dict) and data:
|
||||
out["data"] = data
|
||||
return out
|
||||
|
||||
|
||||
def assert_valid_params(
|
||||
params: Any,
|
||||
validator: Any,
|
||||
method: str,
|
||||
respond: Any,
|
||||
) -> bool:
|
||||
"""Validate request params and respond with a standardized error on failure."""
|
||||
try:
|
||||
ok = bool(validator(params)) if callable(validator) else True
|
||||
except Exception as exc:
|
||||
ok = False
|
||||
err = error_shape("INVALID_REQUEST", f"param validator raised: {type(exc).__name__}")
|
||||
try:
|
||||
respond(False, None, err, None)
|
||||
except Exception:
|
||||
pass
|
||||
return False
|
||||
if ok:
|
||||
return True
|
||||
err = error_shape("INVALID_REQUEST", f"invalid params for {str(method or '').strip() or 'unknown'}")
|
||||
try:
|
||||
respond(False, None, err, None)
|
||||
except Exception:
|
||||
pass
|
||||
return False
|
||||
|
||||
|
||||
__all__ = ["assert_valid_params", "error_shape"]
|
||||
Loading…
Add table
Add a link
Reference in a new issue