Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

165
Visualizações
Node js unable to catch errors when sending an email using AWS SES

I need to be able to catch the error that occurs during the process of sending an email using Express and AWS SES. If there occurs an error, wrong email for example, the API itself shows a CORS error while in nodemon console the error is logged there.

const AWS = require('aws-sdk');

const SES_CONFIG = {
    accessKeyId: '...',
    secretAccessKey: '...',
    region: '...',
};

const AWS_SES = new AWS.SES(SES_CONFIG);

let sendEmail = (recipientEmail, name) => {
    let params = {
        Source: '...@gmail.com',
        Destination: {
            ToAddresses: [
                recipientEmail
            ],
        },
        ReplyToAddresses: [],
        Message: {
            Body: {
                Html: {
                    Charset: 'UTF-8',
                    Data: 'This is the body of my email!',
                },
            },
            Subject: {
                Charset: 'UTF-8',
                Data: `Hello, ${name}!`,
            }
        },
    };
    return AWS_SES.sendEmail(params).promise();
};

The API endpoint:

router.post('/emailTest', async (req, res) => {
    try {
        sendEmail('...@gmail.com', '...', function (err, data) {
            if (err)
                res.send(err);
            res.send(data);
        });

    } catch (error) {
        res.json({
            Status: 500,
            header: 'Error',
            message: 'Internal Server Error. ' + error.message
        });
    } })
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You are calling your sendEmail function with a callback, but your function doesn't use any callbacks. It returns a promise.

Try using await.

router.post('/emailTest', async (req, res) => {
    try {
        const data = await sendEmail('...@gmail.com', '...');
        res.send(data);
    } catch (error) {
        res.json({
            Status: 500,
            header: 'Error',
            message: 'Internal Server Error. ' + error.message
        });
    } })
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda