// return reply() is best practice https://github.com/hapijs/hapi/issues/2168 result.then((response) => { return reply().code(204) }).catch((err) => { switch (err) { case 'NOT_FOUND': reply(Boom.notFound()) break default: reply(new Error(err)) } })
async getJournalEntry(journal, id) { const me = this; try { let entryPath = path.join(me.parentFolder, journal, id); if(!fs.existsSync(entryPath)) throw Boom.notFound(`Entry ${id} not found.`); return JSON.parse(fs.readFileSync(entryPath, 'utf8')) } catch (err) { me.error(scope, 'getJournalEntry', err); throw err; } }
/**++++++++++++++++++++++++++++++* * Static Model Methods **++++++++++++++++++++++++++++++*/ User.defineStatic('findById', function findById (id) { if (_.isEmpty(id)) { return when.reject(Boom.badRequest('User id missing.')) } return this.get(id).run().then(function (user) { if (user) { return when.resolve(user) } return when.reject(Boom.notFound('No user registered with provided credentials.')) }) })
server.route({ method: "POST", path: "/posts/{id}", handler(request, reply) { const post = posts.find(post => post.id == request.params.id); setTimeout(() => { reply(post || Boom.notFound()); }, 1500); }, });
Article.findOne({id: id}).populate('user').exec(function (err, article) { if (err) return reply(err); if (!article) { return reply(Boom.notFound('Article not found')); } article.user = { id: article.user.id, displayName: article.user.displayName, username: article.user.username, email: article.user.email || '' }; reply(article); });
Person .query() .patchAndFetchById(request.params.id, request.payload) .then(function (person) { if (person) { reply(person) return } reply(Boom.notFound('User not found')) }) .catch(function (error) { reply(Boom.badImplementation(error)) })
User .findOneAndUpdate({ _id: id }, request.pre.user, (error, user) => { if (error) { reply(Boom.badRequest(error)) return } if (!user) { reply(Boom.notFound('User id=(' + request.params.id + ') not found!')) return } reply({message: 'User updated!'}) if (request.params.email) { // send verification email to the new email if the email was updated var tokenData = { username: user.username, scope: [user.scope], id: user._id } Common.sentMailVerificationLink(user, createToken(tokenData)) } })
Movie .query() .findById(request.params.id) .then(function (movie) { if (!movie) { throw Boom.notFound('Movie id=(' + request.params.id + ') not found!') } return movie.$relatedQuery('actors') }) .then(function (actors) { reply(actors) }) .catch(function (error) { if (error.isBoom) { reply(error) return } reply(Boom.badImplementation(error)) })
Article.update({id: articleToUpdate.id}, request.payload, function (err, article) { article = article[0]; if (err) { return reply(Boom.badRequest(Errorhandler.getErrorMessage(err))); } else { if (err) return reply(err); if (!article) { return reply(Boom.notFound('Article not found')); } article.user = { id: articleToUpdate.user.id, displayName: articleToUpdate.user.displayName, username: articleToUpdate.user.username, email: articleToUpdate.user.email || '' }; reply(article); } });
// return reply() is best practice https://github.com/hapijs/hapi/issues/2168 result.then((employees) => { return reply(employees) }).catch(err => { if (err === 'NOT_FOUND') { return reply(Boom.notFound()) } else { return reply(new Error(err)) } })
User .find() // Deselect the password and version fields .select('-password -__v') .exec((error, users) => { if (error) { reply(Boom.badRequest(error)) return } if (!users.length) { reply(Boom.notFound('No users found!')) return } reply(users) })
Movie .query() .findById(request.params.id) .then(function (movie) { if (!movie) { throw Boom.notFound('Movie id=(' + request.params.id + ') not found!') } return movie .$relatedQuery('actors') .relate(request.payload.id) }) .then(function () { reply(request.payload) }) .catch(function (error) { if (error.isBoom) { reply(error) return } reply(Boom.badImplementation(error)) })
Person .query() .findById(request.params.id) .then(function (person) { if (!person) { throw Boom.notFound('Person id=(' + request.params.id + ') not found!') } return person .$relatedQuery('children') .insert(request.payload) }) .then(function (child) { reply(child) }) .catch(function (error) { if (error.isBoom) { reply(error) return } reply(Boom.badImplementation(error)) })
result.then((response) => { return reply().code(204) }).catch((err) => { if (err === 'NOT_FOUND') { return reply(Boom.notFound()) } else { return reply(new Error(err)) } })
// return reply() is best practice https://github.com/hapijs/hapi/issues/2168 result.then((employee) => { return reply(employee) }).catch((err) => { if (err === 'NOT_FOUND') { return reply(Boom.notFound()) } else { return reply(new Error(err)) } })