"""handle-error Lambda handler.""" from typing import Any from src import config from src.client import patch_transfer_job def _job_id_from_event(event: dict[str, Any]) -> int: raw_job_id = event["job_id"] if isinstance(raw_job_id, bool): raise ValueError("event['job_id'] must be an integer.") try: return int(raw_job_id) except (TypeError, ValueError) as exc: raise ValueError("event['job_id'] must be an integer.") from exc def _failure_reason_from_event(event: dict[str, Any]) -> str: error = event.get("error") if not isinstance(error, dict): raise ValueError("event['error'] must be an object.") cause = error.get("Cause") if isinstance(cause, str) and cause: return cause error_name = error.get("Error") if isinstance(error_name, str) and error_name: return error_name raise ValueError("event['error'] must include 'Cause' or 'Error'.") def handler(event: dict[str, Any], context: object) -> dict[str, Any]: del context job_id = _job_id_from_event(event) failure_reason = _failure_reason_from_event(event) config.logger.info(f"Handling error for transfer job {job_id}") patch_transfer_job(job_id, {"status": "FAILED", "failure_reason": failure_reason}) config.logger.info(f"Transfer job {job_id} marked FAILED") return {"statusCode": 200}