Skip to content

Docs: Idempotency - Exception not affecting idempotency record sample example has python syntax errors #1216

Closed
@michaelbrewer

Description

@michaelbrewer

Expected Behaviour

Handling exceptions - Exception not affecting idempotency record sample example should not have python syntax errors or missing imports.

Current Behaviour

This example have python syntax errors and missing imports

Screen Shot 2022-05-17 at 9 27 47 PM

Code snippet

def lambda_handler(event, context):
    # If an exception is raised here, no idempotent record will ever get created as the
    # idempotent function does not get called
    do_some_stuff()

    result = call_external_service(data={"user": "user1", "id": 5})

    # This exception will not cause the idempotent record to be deleted, since it
    # happens after the decorated function has been successfully called
    raise Exception


@idempotent_function(data_keyword_argument="data", config=config, persistence_store=dynamodb)
def call_external_service(data: dict, **kwargs):
    result = requests.post('http://example.com', json={"user": data['user'], "transaction_id": data['id']}
    return result.json()

Possible Solution

Fix python syntax errors like the PR #1119 , and deployed over here https://gyft.github.io/aws-lambda-powertools-python/latest/utilities/idempotency/#handling-exceptions

import requests

from aws_lambda_powertools.utilities.idempotency import DynamoDBPersistenceLayer, IdempotencyConfig, idempotent_function

dynamodb = DynamoDBPersistenceLayer(table_name="idem")
config = IdempotencyConfig(event_key_jmespath="order_id")


def lambda_handler(event, context):
    # If an exception is raised here, no idempotent record will ever get created as the
    # idempotent function does not get called
    do_some_stuff()

    result = call_external_service(data={"user": "user1", "id": 5})

    # This exception will not cause the idempotent record to be deleted, since it
    # happens after the decorated function has been successfully called
    raise Exception


@idempotent_function(data_keyword_argument="data", config=config, persistence_store=dynamodb)
def call_external_service(data: dict, **kwargs):
    result = requests.post("http://example.com", json={"user": data["user"], "transaction_id": data["id"]})
    return result.json()

Steps to Reproduce

  1. Go to https://awslabs.github.io/aws-lambda-powertools-python/latest/utilities/idempotency/#handling-exceptions
  2. Copy Exception not affecting idempotency record sample
  3. Code example should not be able to compile

AWS Lambda Powertools for Python version

latest

AWS Lambda function runtime

3.9

Packaging format used

PyPi

Debugging logs

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions