Skip to content
Snippets Groups Projects
Commit 459a97cc authored by ALGLAVE Ivan's avatar ALGLAVE Ivan
Browse files

Added error handling in specific cases

parent ae549c30
No related branches found
No related tags found
1 merge request!2Crud groups
import { Injectable } from '@nestjs/common'; import {
Injectable,
NotFoundException,
InternalServerErrorException,
} from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose'; import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose'; import { Model } from 'mongoose';
import { CreateGroupDto } from '../dto/create-group.dto'; import { CreateGroupDto } from '../dto/create-group.dto';
...@@ -16,6 +20,7 @@ export class GroupsDao { ...@@ -16,6 +20,7 @@ export class GroupsDao {
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
this._groupModel.find({}, {}, {}, (err, value) => { this._groupModel.find({}, {}, {}, (err, value) => {
if (err) reject(err.message); if (err) reject(err.message);
if (!value) reject('No values');
resolve(value); resolve(value);
}); });
}); });
...@@ -24,6 +29,7 @@ export class GroupsDao { ...@@ -24,6 +29,7 @@ export class GroupsDao {
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
this._groupModel.findOne({ id: id }, {}, {}, (err, value) => { this._groupModel.findOne({ id: id }, {}, {}, (err, value) => {
if (err) reject(err.message); if (err) reject(err.message);
if (!value) reject(new NotFoundException());
resolve(value); resolve(value);
}); });
}); });
...@@ -32,6 +38,7 @@ export class GroupsDao { ...@@ -32,6 +38,7 @@ export class GroupsDao {
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
new this._groupModel(group).save((err, value) => { new this._groupModel(group).save((err, value) => {
if (err) reject(err.message); if (err) reject(err.message);
if (!value) reject(new InternalServerErrorException());
resolve(value); resolve(value);
}); });
}); });
...@@ -50,6 +57,7 @@ export class GroupsDao { ...@@ -50,6 +57,7 @@ export class GroupsDao {
}, },
(err, value) => { (err, value) => {
if (err) reject(err.message); if (err) reject(err.message);
if (value.matchedCount === 0) reject(new NotFoundException());
resolve(value); resolve(value);
}, },
); );
......
...@@ -52,7 +52,12 @@ export class HttpInterceptor implements NestInterceptor { ...@@ -52,7 +52,12 @@ export class HttpInterceptor implements NestInterceptor {
tap({ tap({
next: (_) => next: (_) =>
this._logger.log(!!_ ? JSON.stringify(_) : 'NO CONTENT', logCtx), this._logger.log(!!_ ? JSON.stringify(_) : 'NO CONTENT', logCtx),
error: (_) => this._logger.error(_.message, JSON.stringify(_), logCtx), error: (_) =>
this._logger.error(
_?.message ?? 'unspecified error',
JSON.stringify(_),
logCtx,
),
}), }),
); );
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment