mailer: fix error handling

previous mailer code has no callback and thus no way to pass back errors.
now with asyncification it passes back the error
This commit is contained in:
Girish Ramakrishnan
2021-08-19 12:32:23 -07:00
parent ada7166bf8
commit 4cd5137292
10 changed files with 176 additions and 244 deletions

View File

@@ -7,7 +7,7 @@ exports = module.exports = {
getCloudronAvatar
};
var assert = require('assert'),
const assert = require('assert'),
BoxError = require('../boxerror.js'),
HttpError = require('connect-lastmile').HttpError,
HttpSuccess = require('connect-lastmile').HttpSuccess,
@@ -15,44 +15,40 @@ var assert = require('assert'),
settings = require('../settings.js'),
_ = require('underscore');
function getFooter(req, res, next) {
settings.getFooter(function (error, footer) {
if (error) return next(BoxError.toHttpError(error));
async function getFooter(req, res, next) {
const [error, footer] = await safe(settings.getFooter());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { footer }));
});
next(new HttpSuccess(200, { footer }));
}
function setFooter(req, res, next) {
async function setFooter(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.footer !== 'string') return next(new HttpError(400, 'footer is required'));
settings.setFooter(req.body.footer, function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(settings.setFooter(req.body.footer));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
});
next(new HttpSuccess(200, {}));
}
function setCloudronName(req, res, next) {
async function setCloudronName(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.name !== 'string') return next(new HttpError(400, 'name is required'));
settings.setCloudronName(req.body.name, function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(settings.setCloudronName(req.body.name));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, {}));
});
next(new HttpSuccess(202, {}));
}
function getCloudronName(req, res, next) {
settings.getCloudronName(function (error, name) {
if (error) return next(BoxError.toHttpError(error));
async function getCloudronName(req, res, next) {
const [error, name] = await safe(settings.getCloudronName());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { name: name }));
});
next(new HttpSuccess(200, { name }));
}
async function setAppstoreListingConfig(req, res, next) {

View File

@@ -63,7 +63,7 @@ async function login(req, res, next) {
eventlog.add(eventlog.ACTION_USER_LOGIN, auditSource, { userId: req.user.id, user: users.removePrivateFields(req.user) });
await safe(users.notifyLoginLocation(req.user, ip, userAgent, auditSource));
safe(users.notifyLoginLocation(req.user, ip, userAgent, auditSource));
next(new HttpSuccess(200, token));
}

View File

@@ -120,17 +120,16 @@ async function setMailEnabled(req, res, next) {
next(new HttpSuccess(202));
}
function sendTestMail(req, res, next) {
async function sendTestMail(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.body, 'object');
if (!req.body.to || typeof req.body.to !== 'string') return next(new HttpError(400, 'to must be a non-empty string'));
mail.sendTestMail(req.params.domain, req.body.to, function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(mail.sendTestMail(req.params.domain, req.body.to));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202));
});
next(new HttpSuccess(202));
}
async function listMailboxes(req, res, next) {