constructor(code, result, query, values) { const message = errorMessages[code].message; super(message); this.name = this.constructor.name; this.code = code; this.result = result; this.query = query; this.values = values; this.received = result.rows.length; Error.captureStackTrace(this, this.constructor); }
/** * Executes the upgrader's "let's find npm" logic */ async choosePath () { try { const npmPaths = await findNpm(this.options.npmPath) this.log(npmPaths.message) this.options.npmPath = npmPaths.path debug(`Upgrader: Chosen npm path: ${this.options.npmPath}`) } catch (err) { utils.exit(1, err) } }
isEven(value) { const result = Joi.validate(value, boxesSchema); if (result.error) { throw new Error(result.error.details[0].message); } }
getServerErrors(err: { graphQLErrors?: Array<{ message: string }> }) { if (err.graphQLErrors) { const obj = {}; obj.message = err.graphQLErrors[0].message; this.setState({ serverErrors: obj }); } }
//CREATE a user app.post( '/api/users', ( req,res ) => { const result = service.validate( req.body ); if( result.error ) { res.status( 400 ).send( result.error.details[ 0 ].message ); } res.send( service.create( req.body ) ); });
//PUT request Router.put('/:id',(req,res)=>{ const genre = genres.find(genre=>genre.id === parseInt(req.params.id)) if(!genre) return res.status(404).send('The genre with given id not founf'); const {error} = validateGenre(req.body); if(error) return res.status(400).send(error.details[0].message); genre.name = req.body.name; res.send(genre); })
test('standard code quality', function (t) { t.plan(3) const result = healthier.lintTextSync(`a();\n`) t.equal(typeof result, 'object', 'result is an object') t.equal(result.errorCount, 1, '1 error') t.equal(result.results[0].messages[0].message, "'a' is not defined.", 'first message') })
// manual update email router.put('/email', auth, async (req,res,next) => { const {error} = validateEmail(req.body); if(error) return res.status(400).send(error.details[0].message); let user = await User.findOne({email: req.body.email}); if (user) return res.status(400).send('Email already used.') user = await User.findById(req.user._id); if(!user) return res.status(404).send('User account not found.'); user.email = req.body.email; user.save(); res.status(200).send(_.pick(user, ['_id', 'email'])); });
// check if username is unique router.post('/username/unique', async (req,res,next) => { const {error} = validateUsername(_.pick(req.body, ['username'])); if(error) return res.status(400).send(error.details[0].message); let user = await User.findOne({username: req.body.username}); let res_obj = { unique: !user ? true : false } res.status(200).send(res_obj); });
router.put('/:id', (req, res) => { let course = Courses.find(c => c.id === parseInt(req.params.id)) if (!course) { return res.status(404).send(`The Course with this id ${req.params.id} is not available`) } const result = courseValidation(req.body) if (result.error) { return res.status(400).send(result.error.details[0].message) } course.name = req.body.name res.send(JSON.stringify(course)) })
router.post('/', async (req, res) => { const result = validate(req.body) if (result.error) { return res.status(400).send(result.error.details[0].message) } let customer = new Customers({ name: req.body.name, isGold: req.body.isGold, phone: req.body.phone }) customer = await customer.save() res.send(JSON.stringify(customer)) })
app.put('/api/courses/:id', (req, res) => { // Lookup the courses const course = courses.find(c => c.id === parseInt(req.params.id)); if (!course) return res.status(404).send('The course with the given ID was not found'); // Validate - If invalid, return 400 - Bad request const { error } = validateCourse(req.body); // result.error if (error) return res.status(400).send(error.details[0].message); // Update courses course.name = req.body.name; res.send(course); })
getServerErrors(err: { graphQLErrors?: Array<{ message: string }> }) { if (err.graphQLErrors) { const obj = {}; obj.message = err.graphQLErrors[0].message; this.setState({ serverErrors: obj }); } }
app.put('/api/courses/:id', (req, res) => { // Lookup the courses const course = courses.find(c => c.id === parseInt(req.params.id)); if (!course) return res.status(404).send('The course with the given ID was not found'); // Validate - If invalid, return 400 - Bad request const { error } = validateCourse(req.body); // result.error if (error) return res.status(400).send(error.details[0].message); // Update courses course.name = req.body.name; res.send(course); })
test('standard 14 checks', function (t) { t.plan(3) const result = healthier.lintTextSync(`let a = 1\nmodule.exports = a\n`) t.equal(typeof result, 'object', 'result is an object') t.equal(result.errorCount, 1, '1 error') t.equal(result.results[0].messages[0].message, "'a' is never reassigned. Use 'const' instead.", 'first message') })