In Python i have one function that has try and except block in it suppose if i have 3 to 5 errors in that try block i need to capture every error and store it as a dictionary
i created the dictionary to store in it but only single error is storing i need every error to be stored
import io
import pyarrow.parquet as pq
import pandas as pd
import boto3
# AWS credentials and session token
aws_access_key_id = #####
aws_secret_access_key = #####
aws_session_token = #######
region_name = 'us-east-1' # Specify your AWS region here
# Initialize session and clients
session = boto3.session.Session(
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
aws_session_token=aws_session_token,
region_name=region_name
)
s3 = session.client('s3')
sns = session.client('sns')
Topic_Arn = "arn:aws:sns:us-east-1:614414489128:Capturing_Every_Errors"
def read_parquet_from_s3(bucket_name, file_key, s3_client):
error_messages = [] # Initialize an empty list to store error messages
try:
# Read the Parquet file from S3
s3_object = s3_client.get_object(Bucket=bucket_name, Key=file_key)
parquet_content = s3_object['Body'].read()
a = ajay
# Load the Parquet content into a pyarrow table
parquet_table = pq.read_table(io.BytesIO(parquet_content))
b = xyz
# Convert to a Pandas DataFrame
df = parquet_table.to_pandas()
return df
except Exception as e:
error_messages.append(str(e)) # Append error message to list
print(f"An error occurred: {e}")
# Send SNS notification with all accumulated errors
if error_messages:
error_message = "n".join(error_messages)
send_sns_notification(error_message)
return None
def send_sns_notification(message):
try:
sns.publish(
TopicArn=Topic_Arn,
Message=message,
Subject="Error Notification from S3 Parquet Reader"
)
print("SNS Sent Successful")
except Exception as (e):
print(f"Failed to send SNS notification: {e}")
# Example usage
s3_bucket_name = ########
s3_input_key = ########
# Read the Parquet file from S3 and get the DataFrame
df = read_parquet_from_s3(s3_bucket_name, s3_input_key, s3)
if df is not None:
print(df)
else:
print("Failed to read the Parquet file.")`
for this code i need to get the multiple error that should be stored in the error dictionary
New contributor
AJAY RAVICHANDIRAN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.