App: API return errors to js on invalid api methods

This commit is contained in:
Johannes Kirschbauer
2024-12-10 21:49:53 +01:00
parent bf021d25c1
commit 38a47ae2e4
2 changed files with 22 additions and 1 deletions

View File

@@ -124,6 +124,9 @@ class GObjApi:
msg = f"Overwritten method '{m_name}' has different signature than the implementation"
raise ClanError(msg)
def has_obj(self, fn_name: str) -> bool:
return fn_name in self._obj_registry or fn_name in self._methods
def get_obj(self, fn_name: str) -> type[ImplFunc]:
result = self._obj_registry.get(fn_name, None)
if result is not None:

View File

@@ -86,8 +86,27 @@ class WebExecutor(GObject.Object):
log.debug(f"Webview Request: {json_msg}")
payload = json.loads(json_msg)
method_name = payload["method"]
data = payload.get("data")
# Get the function gobject from the api
if not self.api.has_obj(method_name):
self.return_data_to_js(
method_name,
json.dumps(
{
"op_key": data["op_key"],
"status": "error",
"errors": [
{
"message": "Internal API Error",
"description": f"Function '{method_name}' not found",
}
],
}
),
)
return
function_obj = self.api.get_obj(method_name)
# Create an instance of the function gobject
@@ -95,7 +114,6 @@ class WebExecutor(GObject.Object):
fn_instance.await_result(self.on_result)
# Extract the data from the payload
data = payload.get("data")
if data is None:
log.error(
f"JS function call '{method_name}' has no data field. Skipping execution."