fix: replace RuntimeError with StultusUnavailable exception for better error handling

This commit is contained in:
Chipperfluff 2025-07-12 16:32:42 +02:00
parent 9b73e9150b
commit fb0463f5c3

View File

@ -1,37 +1,32 @@
# multinut/stultus.py
import requests
BASE_URL = "https://stultus.chipperfluff.at"
class StultusUnavailable(Exception):
"""Raised when the stultus server is unreachable or returns an error."""
pass
def _get(path: str, params: dict | list = None) -> bool:
try:
r = requests.get(f"{BASE_URL}/{path}", params=params, timeout=5)
r.raise_for_status()
return r.json().get("result", False)
except Exception as e:
raise RuntimeError(f"[STULTUS] Failed logic call to '{path}': {e}")
raise StultusUnavailable(f"[STULTUS] Failed logic call to '{path}': {e}")
def EQUALS(a, b):
return _get("equals", {"a": a, "b": b})
def ping() -> bool:
try:
r = requests.get(f"{BASE_URL}/ping", timeout=2)
return r.status_code == 200 and r.json().get("status") == "ok"
except Exception:
return False
def GREATER(a, b):
return _get("greater", {"a": a, "b": b})
def EQUALS(a, b): return _get("equals", {"a": a, "b": b})
def GREATER(a, b): return _get("greater", {"a": a, "b": b})
def LESSER(a, b): return _get("lesser", {"a": a, "b": b})
def NOT(x): return _get("not", {"val": str(x).lower()})
def AND(*args): return _get("and", [("val", str(v).lower()) for v in args])
def OR(*args): return _get("or", [("val", str(v).lower()) for v in args])
def LESSER(a, b):
return _get("lesser", {"a": a, "b": b})
def AND(*args):
return _get("and", [("val", str(v).lower()) for v in args])
def OR(*args):
return _get("or", [("val", str(v).lower()) for v in args])
def NOT(x):
return _get("not", {"val": str(x).lower()})
def GREATER_EQUAL(a, b):
return OR(GREATER(a, b), EQUALS(a, b))
def LESSER_EQUAL(a, b):
return OR(LESSER(a, b), EQUALS(a, b))
def GREATER_EQUAL(a, b): return OR(GREATER(a, b), EQUALS(a, b))
def LESSER_EQUAL(a, b): return OR(LESSER(a, b), EQUALS(a, b))