zone-eu / zone-mta

📤 Modern outbound MTA cross platform and extendable server application
European Union Public License 1.2
583 stars 93 forks source link

async will make next(err) not work #377

Closed skadefro closed 3 months ago

skadefro commented 3 months ago

I'm not sure if this is a bug or just me not fully understanding everything, but it took me an embarrassingly long time to figure out why I could not 'reject' mails using an error object with next(errObject) or next(app.reject(...)). It turns out (at least for app.addHook('message:store') and app.addHook('message:queue') that if you make the function async, it completely ignores the next() call. My workaround was to simply wrap the function in a setTimeout, and then everything works perfectly again. Works:

    app.addHook('message:store', (envelope, body, next) => {
        setTimeout(async () => {
        }, 0);
    });

calling next(err) does not work

    app.addHook('message:store', async (envelope, body, next) => {
    });

you could/should also just call an async function from within the non async function, but this looks more clean to me.

andris9 commented 3 months ago

When using async functions next is completely ignored and instead, you should use throw err

skadefro commented 3 months ago

ahh, makes sense. thank you.

It is even documented here, i see https://github.com/zone-eu/zone-mta/tree/master/plugins#available-hooks