Skip to content

Commit

Permalink
namespace payload logging msgs
Browse files Browse the repository at this point in the history
  • Loading branch information
leondz committed Oct 2, 2024
1 parent 952a3a1 commit 40f0a79
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions garak/payloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,24 @@ class PayloadGroup:
a number of payload entries"""

def _load(self):
logging.debug("Loading payload %s from %s", self.name, self.path)
logging.debug("payload: Loading payload %s from %s", self.name, self.path)
try:
with open(self.path, "r", encoding="utf-8") as payloadfile:
loaded_payload = json.load(payloadfile)

except FileNotFoundError as fnfe:
msg = "Payload file not found:" + str(self.path)
msg = "payload: file not found:" + str(self.path)
logging.error(msg, exc_info=fnfe)
raise garak.exception.PayloadFailure(msg) from fnfe

except json.JSONDecodeError as jde:
msg = "Payload JSON error:" + str(jde)
msg = "payload: JSON error:" + str(jde)
logging.error(msg, exc_info=jde)
raise garak.exception.PayloadFailure("Payload JSON error") from jde

validation_result = _validate_payload(loaded_payload)
if validation_result is not True:
msg = "Payload JSON schema mismatch:" + str(validation_result)
msg = "payload: JSON schema mismatch:" + str(validation_result)
logging.error(msg, exc_info=validation_result)
raise garak.exception.PayloadFailure(
"Payload didn't match schema"
Expand All @@ -97,7 +97,7 @@ def _load(self):
self.detector_config = dict(loaded_payload["detector_config"])

except TypeError as te:
msg = "Payload detector_config must be a dict, got: " + repr(
msg = "payload: detector_config must be a dict, got: " + repr(
loaded_payload["detector_config"]
)
logging.warning(msg, exc_info=te)
Expand Down Expand Up @@ -147,16 +147,19 @@ def _scan_payload_dir(self, dir) -> dict:
payloads_found = {}
dir = dir
if not dir.is_dir():
logging.debug("payload scan: skipping %s, not dir" % dir)
return {}

logging.debug("payload scan: %s" % dir)

entries = dir.glob("**/*.[jJ][sS][oO][nN]")
for payload_path in entries:
with open(str(payload_path), "r", encoding="utf-8") as payload_path_file:
try:
payload_decoded = json.load(payload_path_file)
payload_types = payload_decoded["payload_types"]
except (json.JSONDecodeError, KeyError) as exc:
msg = f"Invalid payload, skipping: {payload_path}"
msg = f"payload scan: Invalid payload, skipping: {payload_path}"
logging.debug(msg, exc_info=exc)
# raise garak.exception.PayloadFailure(msg) from exc

Expand Down Expand Up @@ -204,12 +207,14 @@ def load(self, name) -> PayloadGroup:
p = load_payload(name, path) # or raise KeyError

except KeyError as ke:
msg = f"Requested payload {name} is not registered in this Director"
msg = (
f"payload: Requested payload {name} is not registered in this Director"
)
logging.error(msg, exc_info=ke)
raise garak.exception.PayloadFailure(msg) from ke

except garak.exception.GarakException as ge:
msg = f"Requested payload {name} not found at expected path {path}"
msg = f"payload: Requested payload {name} not found at expected path {path}"
logging.error(msg, exc_info=ge)
raise garak.exception.PayloadFailure(msg) from ge

Expand Down

0 comments on commit 40f0a79

Please sign in to comment.