使用官方 Node.js SendGrid API 发送电子邮件时,响应 JSON 中可能出现哪些错误消息

What are the possible error messages in the response JSON when sending email using the official Node.js SendGrid API?

本文关键字:JSON 消息 错误 响应 js Node 官方 SendGrid API 电子邮件      更新时间:2023-09-26

我来自Mandrill,它给了我错误消息,例如回调中的successrejectedinvalid对应于下面源代码片段中的回调。例如,在 Mandrill 的"发送"调用中,您可以获得成功的 JSON 响应,例如:

[
    {
        "email": "recipient.email@example.com",
        "status": "sent",
        "reject_reason": "hard-bounce",
        "_id": "abc123abc123abc123abc123abc123"
    }
]

或错误 JSON 响应,例如:

{
    "status": "error",
    "code": 12,
    "name": "Unknown_Subaccount",
    "message": "No subaccount exists with the id 'customer-123'"
}

我在这里看到了来自官方 SendGrid Node.js API 实现的代码以及这个关于 SendGrid Web API v2 的页面,我想知道是否可以有 JSON 响应(在下面的json变量中(,例如 { message: 'error', ... } ,例如当下面的err变量null时。

我的代码看起来像这样(它的灵感来自这个页面(:

sendgrid.send(email, function (err, json) {
    if (err) { return console.error(err); }
    console.log(json);
});

在此代码中,根据我的测试,

  • err可以取值[Error: Missing email body][Error: Missing subject]等或null。当它被null时,则没有错误。
  • json始终具有值 { message: 'success' } 当没有错误时,null 当有错误时

这些是我读过的一些网页,但它们对我没有帮助:

  • 1

我尝试过的谷歌查询:

  • sendgrid message success
  • sendgrid web api response

所以我的问题是,使用官方 SendGrid Node.js API 发送电子邮件时,响应 JSON(在给定的响应 JSON 示例中的 message 属性的值中(中可能存在哪些错误消息?我想到了类似于 Mandrill 的 rejected 的东西,没有使用 webhook。

Mandrill 具有"同步"发送模式,听起来就像您正在使用的。SendGrid没有。我们接受 API 请求(只要它是格式正确的(,然后将其移交进行处理。因此,您的问题的答案是,响应有效负载中没有可能与电子邮件相关的错误,只有请求本身错误的错误。

此终结点的下一个版本将具有更好的验证和错误报告,但仍是完全异步的。但是,响应正文将返回一个消息 ID,稍后可用于检查状态,而无需使用 Webhook。请继续关注。