Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// e.g. '"body" is required' -> 'is required'
const stripFieldName = (str) => str.replace(/^".*?" /, '');
const { source } = error.output.payload.validation;
const validation = error.details.reduce((collector, { path, message }) => {
const field = path[path.length - 1] || source;
return {
...collector,
[field]: (collector[field] || []).concat(stripFieldName(message))
};
}, {});
return Boom.badData(null, { validation });
}
// Handle some specific db errors
if (error instanceof ValidationError) {
return Boom.badData(null, { validation: {} }); // No specifics, avoid leaking model details
}
if (error instanceof NotFoundError) {
return Boom.notFound(`${error.modelName || 'Record'} Not Found`);
}
// Handle all other db errors with avocat
return Avocat.rethrow(error, { return: true, includeMessage: false }) || error;
};
async (email, password, done) => {
let user;
try {
user = await app.locals.services.users.fetch({email});
if (!(await user.isValidPassword(password, user))) {
return done(Boom.badData('', {password: ['is invalid']}));
}
} catch (error) {
if (error instanceof app.locals.bookshelf.NotFoundError) {
return done(Boom.badData('', {email: ['is invalid']}));
}
return done(Boom.unauthorized());
}
return done(null, user);
},
),
if (!wallet) {
return Boom.notFound(`User ${params.userId} could not be found.`);
}
const transactionBuilder = Transactions.BuilderFactory.transfer()
.recipientId(params.recipientId)
.amount(params.amount);
if (params.vendorField) {
transactionBuilder.vendorField(params.vendorField);
}
const transaction: Interfaces.ITransactionData = transactionBuilder.signWithWif(wallet.wif).getStruct();
if (!Transactions.Verifier.verifyHash(transaction)) {
return Boom.badData();
}
await database.set(transaction.id, transaction);
return transaction;
} catch (error) {
return Boom.badImplementation(error.message);
}
},
schema: {
module.exports = errorCatcher(async (req, res, next) => {
const {body: {user: {email, password} = {}} = {}} = req;
if (!email || !password) {
return next(
Boom.badData('', {
...(email ? {} : {email: ['is invalid']}),
...(password ? {} : {password: ['is invalid']}),
}),
);
}
passport.authenticate('local', {session: false}, (error, user) => {
if (error) {
return next(error);
}
req.user = user;
next();
})(req, res, next);
});
module.exports = (err, req, res, next) => {
if (isPostgresUniqueError(err)) {
return next(
Boom.badData('', {
[extractKey(err)]: ['has already been taken'],
}),
);
}
next(err);
};
module.exports = (err, req, res, next) => {
const {app: {locals: {bookshelf} = {}} = {}} = req;
if (err instanceof bookshelf.NotFoundError) {
return next(Boom.notFound());
}
if (
err instanceof bookshelf.NoRowsUpdatedError ||
err instanceof bookshelf.NoRowsDeletedError
) {
return next(Boom.badData());
}
next(err);
};