resend / resend-python

resend's python sdk
MIT License
58 stars 6 forks source link

JSONDecodeError on 503 responses #111

Open itssimon opened 3 weeks ago

itssimon commented 3 weeks ago

I've been getting a few 503 responses from the Resend API when calling resend.Emails.send() recently, with the response body being HTML. This library tries to parse that as JSON and raises a JSONDecodeError. Would be nice if that could be handled more gracefully.

The response body that causes this error looks like this:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta charset="utf-8" />
    <title>Server Error</title>
    ...
drish commented 3 weeks ago

thanks for reporting @itssimon, sure thing, ill double check that.

gustavjohansen98 commented 1 week ago

@drish any updates ?

I'm facing the same issue, and the error info doesn't really help:

Expecting value: line 1 column 1 (char 0)

It's quite critical for our business to be able to send order confirmations, and so far we've been comfortably relying on Resend for that - please help ASAP :))


I pass html as an f string to the html entry of the sendParams, like this:

send_params = {
    'from': 'Company <mail@company.com>',
    'to': ['mail@user.com'],
    'subject': f'Your order is confirmed #{order_number}',
    'html': generate_order_confirmation_html(...),
}

def generate_order_confirmation_html(arg):
    return f'''
    <center style="width: 100%; table-layout: fixed; background-color: #F5F0E6; padding: 20px 0 100px">
        <p>{arg}</p>
    </center>
    '''

Args are always of type str and sometimes I embed a base64 encoded QR code in an img element, however, the JSONDecodeError occurs with and without any base64 embeddings.


Resend version 2.1.0

drish commented 1 week ago

@gustavjohansen98 thanks for sending over your issue as well, a new version should be going out soon (today/tomorrow), that handles unexpected responses from the API more gracefully.

I shared the issue with the rest of the engineering team to investigate why sometimes the API does not return a valid response.