Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnhandledPromiseRejectionWarning: Error: Bad Request #920

Closed
AWR14 opened this issue Apr 24, 2019 · 10 comments
Closed

UnhandledPromiseRejectionWarning: Error: Bad Request #920

AWR14 opened this issue Apr 24, 2019 · 10 comments
Labels
difficulty: unknown or n/a fix is unknown in difficulty status: waiting for feedback waiting for feedback from the submitter type: non-library issue API issue not solvable via the SDK

Comments

@AWR14
Copy link

AWR14 commented Apr 24, 2019

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: 'myemail@email.com',
    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')
  })
})
  • sendgrid-nodejs Version: master (latest commit: [commit number])
    "@sendgrid/mail": "^6.3.1",
  • Node.js Version: v10.14.2
@thinkingserious thinkingserious added difficulty: unknown or n/a fix is unknown in difficulty status: waiting for feedback waiting for feedback from the submitter type: non-library issue API issue not solvable via the SDK labels Apr 24, 2019
@thinkingserious
Copy link
Contributor

Hello @AWR14,

This error happens when you do not handle the promise rejection. Here is an example.

Thanks!

With Best Regards,

Elmer

@hkadyanji
Copy link

@AWR14,
I was facing a similar problem, and I fixed it by making sure the from email field is a valid email

@wolfscott
Copy link

@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"

@vinodkumartheking
Copy link

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

@seehou
Copy link

seehou commented Sep 18, 2019

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: 'recipient@example.org',
  from: 'sender@example.org',
  subject: 'Hello world',
  text: 'Hello plain world!',
  html: '<p>Hello HTML world!</p>',
};

@YEriin
Copy link

YEriin commented Dec 10, 2019

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:)

@childish-sambino
Copy link
Contributor

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.

@ibaldo
Copy link

ibaldo commented Jul 8, 2020

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!

@YoLoADR
Copy link

YoLoADR commented Aug 11, 2020

@AWR14,
I was facing a similar problem, and I fixed it by making sure the from email field is a valid email

Exactly, you have to check the email status inside the sendgrid admin

@sooendre
Copy link

sooendre commented Jun 1, 2021

In my case I was using scheduled email sending, and set scheduled date in milliseconds as opposed to seconds.

The api works with Unix timestamp in seconds(see docs).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty: unknown or n/a fix is unknown in difficulty status: waiting for feedback waiting for feedback from the submitter type: non-library issue API issue not solvable via the SDK
Projects
None yet
Development

No branches or pull requests