common: improve agent error handling
Introduce an AgentError exception which contains error details provided by the agent in response to a client request. These details are included in an "extra" field of the error object in the response. Use code error code 424 (Failed Dependency) when an agent responds to a request with an error. Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
1 parent
9098675ede
commit
1bc2189b72
2 files changed
+23
-18
No files matched your search
+15
-18
@@ -14,8 +14,9 @@ import dataclasses
|
||||
import time
|
||||
from enum import Enum, Flag, auto
|
||||
from abc import ABCMeta, abstractmethod
|
||||
from .utils import ServerError, Sentinel
|
||||
from .utils import Sentinel
|
||||
from .utils import json_wrapper as jsonw
|
||||
from .utils.exceptions import ServerError, AgentError
|
||||
|
||||
# Annotation imports
|
||||
from typing import (
|
||||
@@ -818,14 +819,8 @@ class JsonRPC:
|
||||
result = obj.get("result")
|
||||
if result is None:
|
||||
name = conn.client_data["name"]
|
||||
error = obj.get("error")
|
||||
msg = f"Invalid Response: {obj}"
|
||||
code = -32600
|
||||
if isinstance(error, dict):
|
||||
msg = error.get("message", msg)
|
||||
code = error.get("code", code)
|
||||
msg = f"{name} rpc error: {code} {msg}"
|
||||
ret = ServerError(msg, 418)
|
||||
msg = f"Agent {name} RPC error"
|
||||
ret = AgentError(msg, obj.get("error"))
|
||||
else:
|
||||
ret = result
|
||||
conn.resolve_pending_response(response_id, ret)
|
||||
@@ -846,7 +841,7 @@ class JsonRPC:
|
||||
)
|
||||
except TypeError as e:
|
||||
return self.build_error(
|
||||
-32602, f"Invalid params:\n{e}", req_id, True, method_name
|
||||
-32602, f"Invalid params:\n{e}", req_id, e, method_name
|
||||
)
|
||||
except ServerError as e:
|
||||
code = e.status_code
|
||||
@@ -854,9 +849,9 @@ class JsonRPC:
|
||||
code = -32601
|
||||
elif code == 401:
|
||||
code = -32602
|
||||
return self.build_error(code, str(e), req_id, True, method_name)
|
||||
return self.build_error(code, str(e), req_id, e, method_name)
|
||||
except Exception as e:
|
||||
return self.build_error(-31000, str(e), req_id, True, method_name)
|
||||
return self.build_error(500, str(e), req_id, e, method_name)
|
||||
|
||||
if req_id is None:
|
||||
return None
|
||||
@@ -875,19 +870,21 @@ class JsonRPC:
|
||||
code: int,
|
||||
msg: str,
|
||||
req_id: Optional[int] = None,
|
||||
is_exc: bool = False,
|
||||
exc: Exception | None = None,
|
||||
method_name: str = ""
|
||||
) -> Dict[str, Any]:
|
||||
if method_name:
|
||||
method_name = f"Requested Method: {method_name}, "
|
||||
log_msg = f"JSON-RPC Request Error - {method_name}Code: {code}, Message: {msg}"
|
||||
if is_exc and self.verbose:
|
||||
logging.exception(log_msg)
|
||||
else:
|
||||
logging.info(log_msg)
|
||||
err = {'code': code, 'message': msg}
|
||||
if isinstance(exc, AgentError):
|
||||
err["extra"] = exc.error_data
|
||||
if self.verbose:
|
||||
log_msg += f"\nExtra: {exc.error_data}"
|
||||
logging.info(log_msg, exc_info=(exc is not None and self.verbose))
|
||||
return {
|
||||
'jsonrpc': "2.0",
|
||||
'error': {'code': code, 'message': msg},
|
||||
'error': err,
|
||||
'id': req_id
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,15 @@
|
||||
#
|
||||
# This file may be distributed under the terms of the GNU GPLv3 license
|
||||
|
||||
from __future__ import annotations
|
||||
from typing import Any
|
||||
|
||||
class ServerError(Exception):
|
||||
def __init__(self, message: str, status_code: int = 400) -> None:
|
||||
Exception.__init__(self, message)
|
||||
self.status_code = status_code
|
||||
|
||||
class AgentError(ServerError):
|
||||
def __init__(self, message: str, error_data: Any) -> None:
|
||||
ServerError.__init__(self, message, 424)
|
||||
self.error_data = error_data
|
||||
Reference in new issue
Block a user