Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 64 additions & 7 deletions api/utils/hysds_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,10 +528,39 @@
try:
mozart_response = session.get("{}/job_spec/type?id={}".format(settings.MOZART_V1_URL, job_type), headers=headers,
verify=False)
except Exception as ex:
raise ex
logging.debug("Mozart response status code: {}".format(mozart_response.status_code))

return mozart_response.json()
# Handle 404 - job spec not found
if mozart_response.status_code == 404:
logging.error("Job spec type '{}' not found in Mozart (404)".format(job_type))
raise ValueError("Job specification '{}' not found. Please verify the algorithm is registered.".format(job_type))

# Handle other HTTP errors
if mozart_response.status_code >= 400:
logging.error("Mozart request failed with status {}: {}".format(mozart_response.status_code, mozart_response.text))
raise Exception("Failed to retrieve job specification from Mozart. Status code: {}".format(mozart_response.status_code))

Check warning on line 541 in api/utils/hysds_util.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this generic exception class with a more specific one.

See more on https://sonarcloud.io/project/issues?id=MAAP-Project_maap-api-nasa&issues=AZrsYrlmhc2agEIOOee-&open=AZrsYrlmhc2agEIOOee-&pullRequest=192

# Parse JSON response
try:
response_json = mozart_response.json()
except ValueError as json_err:
logging.error("Failed to parse Mozart response as JSON: {}".format(json_err))
raise Exception("Invalid response from Mozart - could not parse JSON")

Check warning on line 548 in api/utils/hysds_util.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this generic exception class with a more specific one.

See more on https://sonarcloud.io/project/issues?id=MAAP-Project_maap-api-nasa&issues=AZrsYrlmhc2agEIOOee_&open=AZrsYrlmhc2agEIOOee_&pullRequest=192

# Check if response contains result
if not response_json.get("result"):
logging.warning("Mozart returned empty result for job spec type: {}".format(job_type))
raise ValueError("No job specification found for '{}'. The algorithm may not be properly registered.".format(job_type))

logging.debug("Successfully retrieved job spec for type: {}".format(job_type))
return response_json

except requests.exceptions.RequestException as req_ex:
logging.error("Network error while contacting Mozart: {}".format(req_ex))
raise Exception("Failed to connect to Mozart service: {}".format(str(req_ex)))

Check warning on line 560 in api/utils/hysds_util.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this generic exception class with a more specific one.

See more on https://sonarcloud.io/project/issues?id=MAAP-Project_maap-api-nasa&issues=AZrsYrlmhc2agEIOOefA&open=AZrsYrlmhc2agEIOOefA&pullRequest=192
except (ValueError, Exception) as ex:

Check warning on line 561 in api/utils/hysds_util.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this redundant Exception class; it derives from another which is already caught.

See more on https://sonarcloud.io/project/issues?id=MAAP-Project_maap-api-nasa&issues=AZrsYrlmhc2agEIOOefB&open=AZrsYrlmhc2agEIOOefB&pullRequest=192
# Re-raise ValueError and Exception that we've already formatted above
raise ex


def get_hysds_io(hysdsio_type):
Expand All @@ -548,11 +577,39 @@
try:
grq_response = session.get("{}/hysds_io/type?id={}".format(settings.GRQ_URL, hysdsio_type), headers=headers,
verify=False)
logging.debug(grq_response)
except Exception as ex:
raise ex
logging.debug("GRQ response status code: {}".format(grq_response.status_code))

# Handle 404 - algorithm not found
if grq_response.status_code == 404:
logging.error("HySDS-IO type '{}' not found in GRQ (404)".format(hysdsio_type))
raise ValueError("Algorithm specification '{}' not found. Please verify the algorithm is registered.".format(hysdsio_type))

return grq_response.json()
# Handle other HTTP errors
if grq_response.status_code >= 400:
logging.error("GRQ request failed with status {}: {}".format(grq_response.status_code, grq_response.text))
raise Exception("Failed to retrieve algorithm specification from GRQ. Status code: {}".format(grq_response.status_code))

Check warning on line 590 in api/utils/hysds_util.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this generic exception class with a more specific one.

See more on https://sonarcloud.io/project/issues?id=MAAP-Project_maap-api-nasa&issues=AZrsYrlmhc2agEIOOefC&open=AZrsYrlmhc2agEIOOefC&pullRequest=192

# Parse JSON response
try:
response_json = grq_response.json()
except ValueError as json_err:
logging.error("Failed to parse GRQ response as JSON: {}".format(json_err))
raise Exception("Invalid response from GRQ - could not parse JSON")

Check warning on line 597 in api/utils/hysds_util.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this generic exception class with a more specific one.

See more on https://sonarcloud.io/project/issues?id=MAAP-Project_maap-api-nasa&issues=AZrsYrlmhc2agEIOOefD&open=AZrsYrlmhc2agEIOOefD&pullRequest=192

# Check if response contains result
if not response_json.get("result"):
logging.warning("GRQ returned empty result for hysds-io type: {}".format(hysdsio_type))
raise ValueError("No algorithm specification found for '{}'. The algorithm may not be properly registered.".format(hysdsio_type))

logging.debug("Successfully retrieved hysds-io for type: {}".format(hysdsio_type))
return response_json

except requests.exceptions.RequestException as req_ex:
logging.error("Network error while contacting GRQ: {}".format(req_ex))
raise Exception("Failed to connect to GRQ service: {}".format(str(req_ex)))

Check warning on line 609 in api/utils/hysds_util.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this generic exception class with a more specific one.

See more on https://sonarcloud.io/project/issues?id=MAAP-Project_maap-api-nasa&issues=AZrsYrlmhc2agEIOOefE&open=AZrsYrlmhc2agEIOOefE&pullRequest=192
except (ValueError, Exception) as ex:

Check warning on line 610 in api/utils/hysds_util.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this redundant Exception class; it derives from another which is already caught.

See more on https://sonarcloud.io/project/issues?id=MAAP-Project_maap-api-nasa&issues=AZrsYrlmhc2agEIOOefF&open=AZrsYrlmhc2agEIOOefF&pullRequest=192
# Re-raise ValueError and Exception that we've already formatted above
raise ex


def get_recommended_queue(job_type):
Expand Down