Getting the following error:
(node:1475) UnhandledPromiseRejectionWarning: Error: Bad Request
at Request.http [as _callback] (node_modules/@sendgrid/client/src/classes/client.js:124:25)
at Request.self.callback (node_modules/request/request.js:185:22)
at Request.emit (events.js:182:13)
at Request.<anonymous> (node_modules/request/request.js:1161:10)
at Request.emit (events.js:182:13)
at IncomingMessage.<anonymous> (node_modules/request/request.js:1083:12)
at Object.onceWrapper (events.js:273:13)
at IncomingMessage.emit (events.js:187:15)
at endReadableNT (_stream_readable.js:1094:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
(node:1475) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:1475) [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.
My code is in express and node as part of a nextjs app:
const express = require('express')
const next = require('next')
const bodyParser = require('body-parser')
const sgMail = require('@sendgrid/mail');
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
sgMail.setApiKey(myKey);
const send = ({ email, name, text }) => {
const message = {
to: '[email protected]',
from: email,
subject: 'New message from',
text: text,
}
return new Promise((resolve, reject) => {
sgMail.send(message, (error, info) =>
error ? reject(error) : resolve(info)
)
})
}
app.prepare().then(() => {
const server = express()
server.use(bodyParser.json())
server.get('*', (req, res) => {
return handle(req, res)
})
server.post('/api/contact', (req, res) => {
const { email, name, message } = req.body
send(email, name, message)
res.send('success')
})
server.listen(3000, (err) => {
if (err) throw err
console.log('> Read on http://localhost:3000')
})
})
Hello @AWR14,
This error happens when you do not handle the promise rejection. Here is an example.
Thanks!
With Best Regards,
Elmer
@AWR14,
I was facing a similar problem, and I fixed it by making sure the from
email field is a valid email
@hkadyanji, (and anyone else coming along) oddly enough I have discovered that if I do not provide all the fields AND ensure they at least have a space, I get the error -- instead of setting a field to "", I set it to " " (space inside the quotes) and it solved my issue -- I find this quite odd but fixed is better than broken. :) If anyone has any feedback/insight, I'm eager to know...
In fairness, I did not test the to: and from: fields for allowing blanks as they usually have a value in my app so didn't see the need. I will air on the safe side and just ensure any missing field has at least a space until proved otherwise in a future version. But then again, who am I kidding; I'm not likely to visit this code again until something else breaks... lol
I'm using "@sendgrid/mail": "^6.4.0"
did the issue get resolve, i am facing the same error but in different context of bot.
It is working fine in bot emulator but once i publish to azure, i get this error.
BotFrameworkAdapter.processActivity(): 500 ERROR - Error: Bad Request
(node:17240) UnhandledPromiseRejectionWarning: Error: Error: Bad Request
I hit the same warning, but it was my mistake that has a typo issue, instead of text
, I typed it test
.
To make it worked again, make sure the to email & keys are valid.
const msg = {
to: '[email protected]',
from: '[email protected]',
subject: 'Hello world',
text: 'Hello plain world!',
html: '<p>Hello HTML world!</p>',
};
In my case, this error occur is because i did not use await before send
api which result in not catching the error.
And, the error reason is to
and cc
have some intersection。
Remove the repeated emails in to
and cc
may solve your problem:)
There are 2 issues here: making bad requests and not properly handling promise rejections. For the latter, you'll either need to use the send promise result to determine when to respond to your API call or use async/await
. For the former, you'll need to add more debug logic to determine why the request is _bad_. I would start with logging the message object you're attempting to pass in the send
call.
Hello @AWR14,
This error happens when you do not handle the promise rejection. Here is an example.
Thanks!
With Best Regards,
Elmer
Now the new link is this one, actually: https://github.com/sendgrid/sendgrid-nodejs/blob/master/docs/use-cases/success-failure-errors.md ;)
But its a BEAUTIFUL tip cause now its possbile to understand whats is the real error! Thanx!
@AWR14,
I was facing a similar problem, and I fixed it by making sure thefrom
email field is a valid email
Exactly, you have to check the email status inside the sendgrid admin
Most helpful comment
@AWR14,
I was facing a similar problem, and I fixed it by making sure the
from
email field is a valid email