node.js - 达到配额时如何捕获未经授权的 Sendgrid 错误?
问题描述
使用以下简单代码使用 Sendgrid 发送电子邮件没有问题:
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const sendWelcomeEmail = (email, username, code) => {
sgMail.send({
to: email,
from: 'example@gmail.com',
subject: 'Welcome!',
html: '<h3Welcome</h3>'
});
};
然而,当达到 Sendgrid 配额(即每天 100 封免费电子邮件)时,我在服务器上收到此错误:
(node:5060) UnhandledPromiseRejectionWarning: Error: Unauthorized
at Request._callback (C:\...\node_modules\@sendgrid\client\src\classes\client.js:124:25)
[...]
(node:5060) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我一直在不同的地方尝试一些 try catch 块,但没有成功。谢谢。
解决方案
这是 tiguchi 评论,我将其发布为已接受的答案:
这是一个 NodeJS 警告,告诉您向该承诺添加错误处理。只需在发送调用后附加一个 .catch(error => console.error(error)) – tiguchi
推荐阅读
- git - git rebase 和/或 git merge create 是否标记了冲突?
- r - 缺少的类别显示在底部
- list - 得到“致命错误:索引超出范围”:在 swiftui 的列表项中显示索引
- python - pandas 按天、周或月分组以获得时间戳
- ios - 编译 Xamarin iOS
- mongodb - 使用NodeJS时无法连接mongodb服务器
- html - 在数据库中找到 id 时禁用按钮并启用它
- python - 在 Docker 容器中连接 Postgres DB 和模型(Django 和 Flask)
- html - CSS如何获得带有工作的::first-line伪元素的内联显示
- sparql - 即使我没有收到回复,如何在一段时间后停止查询 sparql jena