Improve Error Handling in Case the Product is Not Found

Problem Statement:

Currently, the if [[ -z" $dd_product_pk" ]] condition does not handle errors correctly when it encounters a situation where the product is not found in Defectdojo(wrong API key). Instead of exiting with an appropriate error status (exit code 1), the script continues execution and logs :

null-product.PNG

Consequently, the error message "product not found (check API key)" does not appear in the logs when it should. Instead, the script continues running until it encounters the error message "jq: error (at api_init_findings.json:0): Cannot iterate over null (null)."

if [[ -z "${dd_product_pk}" ]]; then
      log_error "product not found (check API key)"
      exit 1
    fi
    log_info "dd_product_pk: ${dd_product_pk}"

Expected Behavior:

The script should immediately exit with an error status (exit code 1) and display "product not found (check API key)" in the logs to assist in troubleshooting when it cannot find a valid product id.

Steps to Reproduce:

case 1:

  1. Use wrong API key for Defectdojo.
  2. Observe that the script does not exit with an error (exit code 1) but continues execution.
  3. The job failed with error Cannot iterate over null (null).

case 2:

  1. Using correct API key for Defectdojo that doesn't contain the product .
  2. Observe that the script does not exit with an error (exit code 1) but continues execution.
  3. The job failed with error Cannot iterate over null (null).

Proposed Solution:

To address the issue, we can modify the condition since dd_product_pk always becomes 'null' when it doesn't find the product.

 if [[ "$dd_product_pk" == "null" ]]; then
    log_error "Product not found (check API key)"
    exit 1
    fi
    log_info "dd_product_pk: ${dd_product_pk}"

Disclaimer:

It's possible that there may be a specific reason for the existing condition. However, I have a concern about the current implementation, and I would appreciate further clarification or insights if I am mistaken in my understanding.