How to use the @hapi/boom.badData function in @hapi/boom

To help you get started, we’ve selected a few @hapi/boom examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github devinivy / hapipal-realworld-example-app / lib / extensions / error.js View on Github external
// 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;
};
github tanem / express-bookshelf-realworld-example-app / lib / initializers / 05-passport-local.js View on Github external
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);
      },
    ),
github ArkEcosystem / core / deprecated / core-json-rpc / src / server / modules.ts View on Github external
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: {
github tanem / express-bookshelf-realworld-example-app / lib / middleware / local-auth.js View on Github external
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);
});
github tanem / express-bookshelf-realworld-example-app / lib / middleware / handle-postgres-unique-error.js View on Github external
module.exports = (err, req, res, next) => {
  if (isPostgresUniqueError(err)) {
    return next(
      Boom.badData('', {
        [extractKey(err)]: ['has already been taken'],
      }),
    );
  }

  next(err);
};
github tanem / express-bookshelf-realworld-example-app / lib / middleware / handle-bookshelf-error.js View on Github external
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);
};