1
0

More than 3 years have passed since last update.

【boto3】AWS SQS send_messages のエラー処理

Posted at

send_messages のエラー処理について

問題点

The result of sending each message is reported individually in the response. Because the batch request can result in a combination of successful and unsuccessful actions, you should check for batch errors even when the call returns an HTTP status code of 200 .

send_messages - Boto3 Docs

DeepL翻訳

各メッセージの送信結果は、レスポンスで個別に報告されます。バッチリクエストは、成功したアクションと失敗したアクションの組み合わせになる可能性があるため、HTTP ステータスコードが 200 である場合でも、バッチエラーをチェックする必要があります。

  • 一部失敗(10件中2件など)時 HTTP status code が 200 で返ることがある
    • つまり失敗していても例外が発生しないことがある

対応方法

before

import boto3

sqs = boto3.resource('sqs')
queue = sqs.Queue('url')

entries = [{'Id': f'{i:08}', 'MessageBody': 'Message',} i for i in range(10)]

try:
    # 10件エンキュー
    enque_result = sqs.send_messages(entries)
except:
    # 例外処理

after

import boto3

sqs = boto3.resource('sqs')
queue = sqs.Queue('url')

entries = [{'Id': f'{i:08}', 'MessageBody': 'Message'} for i in range(10)]

try:
    # 10件エンキュー
    enque_result = sqs.send_messages(entries)

    # 一部失敗時用 <- これを追加
    if enque_result['Failed']:
        # 例外処理

# 全部失敗用
except:
    # 例外処理

ここまで書いといてアレだが数百万回エンキューしても一部失敗で返ったことがないので実際にこのコードで問題がないかは分からない

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0