From 459a97ccc0e54bb76f867399bd133d67799f52fd Mon Sep 17 00:00:00 2001
From: ALGLAVE Ivan <ivan.alglave8@etu.univ-lorraine.fr>
Date: Thu, 10 Nov 2022 15:38:45 +0100
Subject: [PATCH] Added error handling in specific cases

---
 src/groups/dao/groups.dao.ts         | 10 +++++++++-
 src/interceptors/http.interceptor.ts |  7 ++++++-
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/src/groups/dao/groups.dao.ts b/src/groups/dao/groups.dao.ts
index 885b2f5..682c742 100644
--- a/src/groups/dao/groups.dao.ts
+++ b/src/groups/dao/groups.dao.ts
@@ -1,4 +1,8 @@
-import { Injectable } from '@nestjs/common';
+import {
+  Injectable,
+  NotFoundException,
+  InternalServerErrorException,
+} from '@nestjs/common';
 import { InjectModel } from '@nestjs/mongoose';
 import { Model } from 'mongoose';
 import { CreateGroupDto } from '../dto/create-group.dto';
@@ -16,6 +20,7 @@ export class GroupsDao {
     new Promise((resolve, reject) => {
       this._groupModel.find({}, {}, {}, (err, value) => {
         if (err) reject(err.message);
+        if (!value) reject('No values');
         resolve(value);
       });
     });
@@ -24,6 +29,7 @@ export class GroupsDao {
     new Promise((resolve, reject) => {
       this._groupModel.findOne({ id: id }, {}, {}, (err, value) => {
         if (err) reject(err.message);
+        if (!value) reject(new NotFoundException());
         resolve(value);
       });
     });
@@ -32,6 +38,7 @@ export class GroupsDao {
     new Promise((resolve, reject) => {
       new this._groupModel(group).save((err, value) => {
         if (err) reject(err.message);
+        if (!value) reject(new InternalServerErrorException());
         resolve(value);
       });
     });
@@ -50,6 +57,7 @@ export class GroupsDao {
         },
         (err, value) => {
           if (err) reject(err.message);
+          if (value.matchedCount === 0) reject(new NotFoundException());
           resolve(value);
         },
       );
diff --git a/src/interceptors/http.interceptor.ts b/src/interceptors/http.interceptor.ts
index ebef03f..934929f 100644
--- a/src/interceptors/http.interceptor.ts
+++ b/src/interceptors/http.interceptor.ts
@@ -52,7 +52,12 @@ export class HttpInterceptor implements NestInterceptor {
       tap({
         next: (_) =>
           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,
+          ),
       }),
     );
   };
-- 
GitLab