RogyBear
RogyBear

Reputation: 93

Strapi Email Returns AssertionError [ERR_ASSERTION]: Cannot wrap non-Error object

I am using Strapi and can't get the email route to work in development. When I hit the /email route in postman, it returns internal service error. In the terminal I get the following error AssertionError [ERR_ASSERTION]: Cannot wrap non-Error object.

I have been following the docs here https://strapi.io/documentation/3.0.0-beta.x/migration-guide/migration-guide-alpha.26-to-beta.html#migrating-plugins as well as referencing an article here

What's odd is that when I followed the docs to set up the built in strapi email plugin in another project, everything worked fine. In the new project I'm working on, I more or less copied that code from my previous project and put it in the new project. But in the new project the /email route doesn't seem to work.

Here is the code that I'm using in extensions/email/controllers/Email.js

module.exports = {
    send: async (ctx) => {
        // Retrieve provider configuration.

        const config = await strapi
            .store({
                environment: strapi.config.environment,
                type: 'plugin',
                name: 'email'
            })
            .get({ key: 'provider' });

        // Verify if the file email is enable.
        if (config.enabled === false) {
            strapi.log.error('Email is disabled');
            return ctx.badRequest(
                null,
                ctx.request.admin ? [ { messages: [ { id: 'Email.status.disabled' } ] } ] : 'Emailis disabled'
            );
        }

        // Something is wrong
        if (ctx.status === 400) {
            return;
        }
        let options = ctx.request.body;
        await strapi.plugins.email.services.email.send(options, config);

        await strapi.plugins['email'].services.email.send({
            to: ctx.request.body.to,
            subject: 'Test message',
            text: 'Test Text'
        });
    }
};

If anyone needs anymore information, or needs me to clarify something, please let me know. Thanks!

Upvotes: 1

Views: 1989

Answers (1)

Kundan Kumar
Kundan Kumar

Reputation: 401

Put a try and catch in the email send service, you will be able to get the actual error behind this, as in my case when I was using SendGrid as the mail provider I did the same approach and found this in the error body

status:403,
[
  {
    message: 'The from address does not match a verified Sender Identity. Mail cannot be sent until this error is resolved. Visit https://sendgrid.com/docs/for-developers/sending-email/sender-identity/ to see the Sender Identity requirements',
    field: 'from',
    help: null
  }
]

This got fixed after putting a verified email in the default_from key which i generated from the SendGrid dashboard panel.

Upvotes: 1

Related Questions