Skip to content

Commit

Permalink
GUI-4971 Add non_empty_string type for required params, display inval…
Browse files Browse the repository at this point in the history
…id JSON response for debugging purpose
  • Loading branch information
cristianp-fossid committed Feb 29, 2024
1 parent 63d49f0 commit 83ec9d9
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions workbench-agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,17 @@ def _send_request(self, payload: dict) -> dict:
"POST", url, headers=headers, data=req_body, timeout=1800
)
logger.debug(response.text)
return json.loads(response.text)
try:
# Attempt to parse the JSON
parsed_json = json.loads(response.text)
return parsed_json
except json.JSONDecodeError as e:
# If an error occurs, catch it and display the message along with the problematic JSON
print("Failed to decode JSON")
print(f"Error message: {e.msg}")
print(f"At position: {e.pos}")
print("Problematic JSON:")
print(response.text)

def upload_files(self, scan_code: str, path: str):
"""
Expand Down Expand Up @@ -818,6 +828,13 @@ def parse_cmdline_args():
Returns:
argparse.Namespace: An object containing the parsed command line arguments.
"""

# Define a custom type function which will verify for empty string
def non_empty_string(s):
if not s.strip():
raise argparse.ArgumentTypeError("Argument cannot be empty or just whitespace.")
return s

parser = argparse.ArgumentParser(
add_help=False,
description="Run FossID Workbench Agent",
Expand All @@ -838,33 +855,33 @@ def parse_cmdline_args():
required.add_argument(
"--api_url",
help="URL of the Workbench API instance, Ex: https://myserver.com/api.php",
type=str,
type=non_empty_string,
required=True,
)
required.add_argument(
"--api_user",
help="Workbench user that will make API calls",
type=str,
type=non_empty_string,
required=True,
)
required.add_argument(
"--api_token",
help="Workbench user API token (Not the same with user password!!!)",
type=str,
type=non_empty_string,
required=True,
)
required.add_argument(
"--project_code",
help="Name of the project inside Workbench where the scan will be created.\n"
"If the project doesn't exist, it will be created",
type=str,
type=non_empty_string,
required=True,
)
required.add_argument(
"--scan_code",
help="The scan code user when creating the scan in Workbench. It can be based on some env var,\n"
"for example: ${BUILD_NUMBER}",
type=str,
type=non_empty_string,
required=True,
)
optional.add_argument(
Expand Down

0 comments on commit 83ec9d9

Please sign in to comment.