safely check other nested json lighthouse scores
@@ -38,14 +38,14 @@ class LighthouseCheck(AgentCheck):
raise CheckException(error_message, exit_code, e)
if data.get("runtimeError", {"code": EXPECTED_RESPONSE_CODE}).get("code") == EXPECTED_RESPONSE_CODE:
- score_accessibility = round(data["categories"]["accessibility"]["score"] * 100)
- score_best_practices = round(data["categories"]["best-practices"]["score"] * 100)
- score_performance = round(data["categories"]["performance"]["score"] * 100)
- score_pwa = round(data["categories"]["pwa"]["score"] * 100)
- score_seo = round(data["categories"]["seo"]["score"] * 100)
+ score_accessibility = round(data.get("categories", {}).get("accessibility", {}).get("score", 0) * 100)
+ score_best_practices = round(data.get("categories", {}).get("best-practices", {}).get("score", 0) * 100)
+ score_performance = round(data.get("categories", {}).get("performance", {}).get("score", 0) * 100)
+ score_pwa = round(data.get("categories", {}).get("pwa", {}).get("score", 0) * 100)
+ score_seo = round(data.get("categories", {}).get("seo", {}).get("score", 0) * 100)
else:
- err_code = data["runtimeError"]["code"]
- err_msg = data["runtimeError"]["message"]
+ err_code = data.get("runtimeError", {}).get("code")
+ err_msg = data.get("runtimeError", {}).get("message")
self.log.warn("not collecting lighthouse metrics for url {0} runtimeError code {1} message {2}"
.format(lighthouse_url, err_code, err_msg)
)
