How to use the Error function from mongoose
Find comprehensive JavaScript mongoose.Error code examples handpicked from public code repositorys.
GitHub: plaso/wallahack-test-api
31 32 33 34 35 36 37 38 39 40 41
app.use((error, req, res, next) => { console.error(error); if (error instanceof mongoose.Error.ValidationError) { error = createError(StatusCodes.BAD_REQUEST, error); } else if (error instanceof mongoose.Error.CastError) { error = createError(StatusCodes.NOT_FOUND, 'Resource not found'); } else if (error.message.includes('E11000')) { error = createError(StatusCodes.BAD_REQUEST, 'Already exists'); } else if (error instanceof jwt.JsonWebTokenError) {
0
0
1
+ 5 other calls in file
48 49 50 51 52 53 54 55 56 57
.then((user) => res.status(HTTP_STATUS_CREATED).send(user.clean())) // данные не записались, вернём ошибку .catch((err) => { if (err.code === 11000) { next(new ConflictError('Пользователь с данным email уже существует')); } else if (err instanceof mongoose.Error.ValidationError) { next(new BadRequestError(err.message)); } else { next(err); }
0
0
0
+ 14 other calls in file
6 7 8 9 10 11 12 13 14 15
const errorHandler = async (err, req, res, next) => { if (err instanceof ObjectDoesNotExist) { return res.status(httpStatus.NOT_FOUND) .send({ message: err.message }); } if (err instanceof mongoose.Error.ValidationError) { return res.status(httpStatus.BAD_REQUEST) .send({ message: 'Данные не прошли валидацию' }); } if (err instanceof mongoose.Error.CastError) {
0
0
0
+ 3 other calls in file
38 39 40 41 42 43 44 45 46 47 48 49
}) app.use((error, req, res, next) => { console.log(error) if(error instanceof mongoose.Error.ValidationError) { error = createError(StatusCodes.BAD_REQUEST, error) } else if (error instanceof mongoose.Error.CastError) { error = createError(StatusCodes.BAD_REQUEST, 'Resource not found') } else if (error.message.includes('E11000')) {
0
0
0
+ 3 other calls in file
57 58 59 60 61 62 63 64 65 66
throw new NotFoundError('Пользователь по указанному _id не найден'); }) .then((user) => res.send(user)) // eslint-disable-next-line consistent-return .catch((error) => { if (error instanceof mongoose.Error.CastError) { return next(new BadRequestError('Переданы некорректные данные')); } next(error); });
0
0
0
+ 3 other calls in file
22 23 24 25 26 27 28 29 30 31 32
app.use((req, res, next) => next(createError(404, 'Route not found'))) app.use((error, req, res, next) => { if (error instanceof mongoose.Error.ValidationError) { error = createError(400, error); } else if (error instanceof mongoose.Error.CastError && error.path === '_id') { const resourceName = error.model().constructor.modelName; error = createError(404, `${resourceName} not found`); } else if (error.message.includes("E11000")) { // Duplicate key
0
0
0
mongoose.model is the most popular function in mongoose (2160 examples)