Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnboundLocalError from index.handler during deployment #668

Closed
MustaphaU opened this issue Dec 1, 2024 · 2 comments
Closed

UnboundLocalError from index.handler during deployment #668

MustaphaU opened this issue Dec 1, 2024 · 2 comments

Comments

@MustaphaU
Copy link
Contributor

MustaphaU commented Dec 1, 2024

When deploying the Retail Demo Store, the following error is encountered when retries are exhausted during the execution of the handler function in the LoadDataLambdaFunction in deployment-support.yaml.

Error

 UnboundLocalError: cannot access local variable 'e' where it is not associated with a value
 Traceback (most recent call last):
   File "/var/task/index.py", line 31, in handler
     logger.error(f"Error calling product service init: {e.code} : {e.reason}")

The relevant code block:

...
while retries < max_retries:
      try:
          with urlopen(request) as response:
              logger.info(f"Product Service init method success: {response.read()}")
              # exit while, success.
              break
      except URLError as e:
          retries += 1
          print(f"Request failed. Retrying in 30 seconds... (Attempt {retries}/{max_retries})")
          print(f"Error: {e}")
          time.sleep(30)

#
if retries >= max_retries:
    logger.error(f"Error calling product service init: {e.code} : {e.reason}")
    response_status = cfnresponse.FAILED
    response_data['Message'] = f"Resource {event['RequestType']} failed: {e}"  
...      

The scope of e is local to the except block and Python does not persist it outside that scope. The condition if retries >= max_retries runs outside the loop and, therefore, cannot access e. The value (e) within the block can be assigned to a variable to be accessed outside.

@MustaphaU MustaphaU changed the title UnboundLocalError in index.handler (defined in depoyment-support.yaml) during deployment UnboundLocalError from index.handler during deployment Dec 1, 2024
@berry2012
Copy link

I got the same error as well

@log	
XXXXXXXXXX:/aws/lambda/retaildemostore-DeploymentS-LoadDataLambdaFunction-Yb5K2S2Y3jVk
@logStream	
2024/12/02/[$LATEST]c0262b725f6646148b67ed17c687ca39
@message	
[ERROR] UnboundLocalError: cannot access local variable 'e' where it is not associated with a value
Traceback (most recent call last):
  File "/var/task/index.py", line 31, in handler
    logger.error(f"Error calling product service init: {e.code} : {e.reason}")

@BastLeblanc
Copy link
Contributor

BastLeblanc commented Dec 4, 2024

Thanks for the report.
The underline issue on the product service is fixed by #670

Thanks for the PR #669 will review it asap

@BastLeblanc BastLeblanc moved this from Triage to Backlog in Retail Demo Store Dec 4, 2024
@BastLeblanc BastLeblanc added help wanted Extra attention is needed and removed help wanted Extra attention is needed labels Dec 4, 2024
@BastLeblanc BastLeblanc moved this from Backlog to Pending review in Retail Demo Store Dec 4, 2024
@github-project-automation github-project-automation bot moved this from Pending review to Shipped in Retail Demo Store Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Shipped
Development

No branches or pull requests

3 participants