Home > Blockchain >  I'm getting Unable to marshal response: Object of type ClientError is not JSON serializable dur
I'm getting Unable to marshal response: Object of type ClientError is not JSON serializable dur

Time:10-18

I've a lambda function which runs a python script and the script basically reads data from one dynamodb table and write to another dynamodb table. While executing the script for the first time it's throwing me this error -

Lambda function (abc-Service-295-DummyFunction) returned error: ({"errorMessage": "Unable to marshal response: Object of type ClientError is not JSON serializable", "errorType": "Runtime.MarshalError", "stackTrace": []})

I found some stackoverflow answers related to this and mostly it's being said the error might be coming from a Datetime field. The source table do have some attributes with Datetime form but I'm not sure for my script how can I avoid this error

Here is my code -

import pprint
import boto3
from boto3.dynamodb.conditions import Key, Attr

def dynamo_bulk_reader(event):
    dynamodb = boto3.resource('dynamodb', region_name=event['TargetRegion'])
    table = dynamodb.Table(event['EnvironmentId'] '-abc-dynamo')

    print("Exporting items from: "   str(table))
    data = []
    
    response = table.query(
        IndexName = 'idx-RecType-SubsId',
        KeyConditionExpression = Key('RecType').eq('CreditNote') & Key('SubsId').begins_with(str(event['BuisnessUnitId']) '_'),
        FilterExpression = Attr('Split').eq(0)
    )

    for i in range(len(response['Items'])):
        if(abs(response['Items'][i]['CNAttr']['RemBal']) > 0):
            data.append(response['Items'][i])

    print("Finished exporting: "   str(len(data))   " items.")
    print("Data Exported: ")
    pprint.pprint(data)
    return data

def dynamo_bulk_writer(event):
    dynamodb = boto3.resource('dynamodb', region_name=event['TargetRegion'])
    table = dynamodb.Table(event['EnvironmentId'] '-xyz-dynamo2')

    print("Importing items into: "   str(table))

    for table_item in dynamo_bulk_reader(event):
        with table.batch_writer() as batch:
            response = batch.put_item(
                Item = {
                    'SubsId'  : table_item['SubsId'],
                    'ItemId'  : table_item['ItemId'],
                    'RecType' : table_item['RecType'],
                    'BuId'    : int(table_item['SubsId'].split("_")[0])
                }
            )
    print("Finished importing items...")

def lambda_handler(event,context):
    try:
        print("Event Received", event)
        dynamo_bulk_writer(event)
    except Exception as e:
        return e

Can someone help me on this? How can I get rid of this error?

CodePudding user response:

One of your API calls to DynamoDB is returning an error, but instead of catching it you try to parse it causing the exception. Try to implement error handling in any API requests that you make:

try:
    response = table.query(
        IndexName = 'idx-RecType-SubsId',
        KeyConditionExpression = Key('RecType').eq('CreditNote') & Key('SubsId').begins_with(str(event['BuisnessUnitId']) '_'),
        FilterExpression = Attr('Split').eq(0)
    )

except Exception as e:
    print(e)

Add the same try/except block to all your requests and that should allow you to see the true failure reason.

  • Related