main ← nul800sebastiaan:fix-sendgrid-webhook
opened 09:27AM - 23 Feb 25 UTC
As described in https://meta.discourse.org/t/sendgrid-nomethoderror-undefined-me…thod-for-nil-nilclass/285221/9?u=cultiv
When an email bounced because the host does not exist (any more), the payload of the webhook Sendgrid sends does not include a `status` field, leading to an error: `NoMethodError (undefined method `[]' for nil)`.
The payload is as follows:
```json
[
{
"bounce_classification": "Unclassified",
"email": "noemail@this.does.not.exist.tld",
"event": "bounce",
"reason": "unable to get mx info: failed to get IPs from PTR record: lookup <nil>: unrecognized address",
"sg_event_id": "Ym91bmNlLTQtNTA0ODUxOTUtZXVvMmlLeGRTYXlQRjRZRTQtLUk3QS0w",
"sg_message_id": "euo2iKxdSayPF4YE4--I7A.recvd-5f54b5d587-pczjm-1-67BADEEA-6.0",
"smtp-id": "<870b3a2a-160c-4fc8-bc9a-bd0d5b943b81@forum.umbraco.com>",
"timestamp": 1740300320,
"tls": 0,
"type": "blocked"
}
]
```
I figured `"type": "blocked"` is probably a good thing to test on and set the `error_code` for. I feel like this is probably an unrecoverable error, so I turned it into a `5.5.0` error, just like when an email on a valid domain bounces.
Furthermore, before reading the `error_code`, I added a check for `nil`, just in case the variable has still not been set for some reason.
Word of warning: this is my first time writing Ruby code, so please correct me if I am doing it wrong 😅