Skip to content

Commit

Permalink
refactor : more generic error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
hebertzin committed Mar 24, 2024
1 parent f529ef2 commit 3e80593
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 38 deletions.
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
18.16.0
4 changes: 2 additions & 2 deletions src/comments/services/comments/comments.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { PrismaService } from 'src/database/prisma.service';
import { UserService } from 'src/user/services/user/user.service';
import { ProjectsService } from 'src/projects/services/projects/projects.service';
import { Comments } from '@prisma/client';
import { errors } from 'src/helpers/errors';
import { Errors } from 'src/helpers/errors';
import { LoggerService } from 'src/logger/logger.service';

@Injectable()
Expand Down Expand Up @@ -47,7 +47,7 @@ export class CommentsService {
return comment;
} catch (error) {
if (error instanceof NotFoundException) {
throw new NotFoundException(errors.commentDoesNotExist);
throw new NotFoundException(Errors.RESOURCE_NOT_FOUND, comment_id);
}

this.logger.error(`some error ocurred : ${error.message}`);
Expand Down
4 changes: 2 additions & 2 deletions src/decisions/services/decisions/decisions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Decision } from 'src/decisions/types/decision';
import { ProjectsService } from 'src/projects/services/projects/projects.service';
import { UserService } from 'src/user/services/user/user.service';
import { Decisions } from '@prisma/client';
import { errors } from 'src/helpers/errors';
import { Errors } from 'src/helpers/errors';
import { LoggerService } from 'src/logger/logger.service';

@Injectable()
Expand Down Expand Up @@ -45,7 +45,7 @@ export class DecisionsService {
return decision;
} catch (error) {
if (error instanceof NotFoundException) {
throw new NotFoundException(errors.decisionDoesNotExist);
throw new NotFoundException(Errors.RESOURCE_NOT_FOUND, decision_id);
}
this.logger.error(`some error ocurred : ${error.message}`);
throw error;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ConflictException, Injectable } from '@nestjs/common';
import { PrismaService } from 'src/database/prisma.service';
import { Data } from 'src/follow-project/types/follow';
import { errors } from 'src/helpers/errors';
import { Errors } from 'src/helpers/errors';
import { LoggerService } from 'src/logger/logger.service';
import { ProjectsService } from 'src/projects/services/projects/projects.service';
import { UserService } from 'src/user/services/user/user.service';
Expand Down Expand Up @@ -35,7 +35,10 @@ export class FollowProjectService {
return userAlreadyFollowProject;
} catch (error) {
if (error instanceof ConflictException) {
throw new ConflictException(errors.alreadyFollowProject);
throw new ConflictException(
Errors.RESOURCE_ALREADY_EXISTS,
`${userId} - ${projectId}`,
);
}
this.logger.error(`some error ocurred : ${error}`);
throw error;
Expand Down
4 changes: 2 additions & 2 deletions src/goals/services/goals/goals.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { TGoals } from 'src/goals/types/gaols';
import { ProjectsService } from 'src/projects/services/projects/projects.service';
import { UserService } from 'src/user/services/user/user.service';
import { Goals } from '@prisma/client';
import { errors } from 'src/helpers/errors';
import { Errors } from 'src/helpers/errors';
import { LoggerService } from 'src/logger/logger.service';

@Injectable()
Expand Down Expand Up @@ -81,7 +81,7 @@ export class GoalsService {
return goal;
} catch (error) {
if (error instanceof NotFoundException) {
throw new NotFoundException(errors.goalDoesNotExist);
throw new NotFoundException(Errors.RESOURCE_NOT_FOUND, goal_id);
}
this.logger.error(`some error ocurred : ${error.message}`);
throw error;
Expand Down
17 changes: 4 additions & 13 deletions src/helpers/errors.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
export const errors = {
projectDoesNotExist: 'project does not exist',
userDoesNotExist: 'user does not exist',
userAlreadyExist: 'user already exist',
decisionDoesNotExist: 'decision does not exist',
profileDoesNotExist: 'profile does not exist',
commentDoesNotExist: 'comment does not exist',
goalDoesNotExist: 'goal does not exist',
updateDoesNotExist: 'update does not exist',
questionDoesNotExist: 'question does not exist',
projectIdeaDoesNotExist: 'project idea does not exist',
alreadyFollowProject: 'you already follow this project',
teamDoesNotExist: 'team does not exist',
export const Errors = {
RESOURCE_NOT_FOUND: 'Recurso não encontrado no banco de dados',
RESOURCE_ALREADY_EXISTS: 'Recurso já existe',
MISSING_RESOURCE: 'Recurso ausente',
};
4 changes: 2 additions & 2 deletions src/profile/services/profile/profile.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PrismaService } from 'src/database/prisma.service';
import { TProfile } from 'src/profile/types/profile';
import { UserService } from 'src/user/services/user/user.service';
import { Profile } from '@prisma/client';
import { errors } from 'src/helpers/errors';
import { Errors } from 'src/helpers/errors';
import { LoggerService } from 'src/logger/logger.service';

@Injectable()
Expand All @@ -28,7 +28,7 @@ export class ProfileService {
return profile;
} catch (error) {
if (error instanceof NotFoundException) {
throw new NotFoundException(errors.profileDoesNotExist);
throw new NotFoundException(Errors.RESOURCE_NOT_FOUND, profile_id);
}
this.logger.error(`some error ocurred : ${error.message}`);
throw error;
Expand Down
4 changes: 2 additions & 2 deletions src/project-idea/services/project-idea.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable, NotFoundException } from '@nestjs/common';
import { PrismaService } from 'src/database/prisma.service';
import { ProjectIdea } from '../types/projectIdea';
import { UserService } from 'src/user/services/user/user.service';
import { errors } from 'src/helpers/errors';
import { Errors } from 'src/helpers/errors';
import { LoggerService } from 'src/logger/logger.service';

@Injectable()
Expand Down Expand Up @@ -56,7 +56,7 @@ export class ProjectIdeaService {
return projectIdea;
} catch (error) {
if (error instanceof NotFoundException) {
throw new NotFoundException(errors.projectIdeaDoesNotExist);
throw new NotFoundException(Errors.RESOURCE_NOT_FOUND, id);
}
this.logger.error(`some errror ocurred : ${error.message}`);
throw error;
Expand Down
4 changes: 2 additions & 2 deletions src/projects/services/projects/projects.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PrismaService } from 'src/database/prisma.service';
import { Project } from 'src/projects/types';
import { UserService } from 'src/user/services/user/user.service';
import { Projects } from '@prisma/client';
import { errors } from 'src/helpers/errors';
import { Errors } from 'src/helpers/errors';
import { LoggerService } from 'src/logger/logger.service';

@Injectable()
Expand All @@ -29,7 +29,7 @@ export class ProjectsService {
return project;
} catch (error) {
if (error instanceof NotFoundException) {
throw new NotFoundException(errors.projectDoesNotExist);
throw new NotFoundException(Errors.RESOURCE_NOT_FOUND, project_id);
}
this.logger.error(
`some error ocurred checking project existence : ${error.message}`,
Expand Down
4 changes: 2 additions & 2 deletions src/questions/services/questions/questions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PrismaService } from 'src/database/prisma.service';
import { TQuestion } from 'src/questions/types/question';
import { UserService } from 'src/user/services/user/user.service';
import { Questions } from '@prisma/client';
import { errors } from 'src/helpers/errors';
import { Errors } from 'src/helpers/errors';
import { LoggerService } from 'src/logger/logger.service';

@Injectable()
Expand Down Expand Up @@ -38,7 +38,7 @@ export class QuestionsService {
},
});
if (!question) {
throw new NotFoundException(errors.questionDoesNotExist);
throw new NotFoundException(Errors.RESOURCE_NOT_FOUND, question_id);
}
return question;
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions src/send-email/service/send-email/send-email.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { MailerService } from '@nestjs-modules/mailer';
import { Injectable, NotFoundException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { PrismaService } from 'src/database/prisma.service';
import { errors } from 'src/helpers/errors';
import { Errors } from 'src/helpers/errors';
import { LoggerService } from 'src/logger/logger.service';

@Injectable()
Expand Down Expand Up @@ -48,7 +48,7 @@ export class SendEmailService {
});

if (!user) {
throw new NotFoundException(errors.userDoesNotExist);
throw new NotFoundException(Errors.RESOURCE_NOT_FOUND);
}

await this.prismaService.user.update({
Expand Down
9 changes: 9 additions & 0 deletions src/task-list-project/task-list-project.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { TaskListProjectController } from './controller/task-list-project/task-list-project.controller';
import { TaskListProjectService } from './service/task-list-project/task-list-project.service';

@Module({
controllers: [TaskListProjectController],
providers: [TaskListProjectService],
})
export class TaskListProjectModule {}
4 changes: 2 additions & 2 deletions src/team/services/team/team.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { Team } from '@prisma/client';
import { PrismaService } from 'src/database/prisma.service';
import { errors } from 'src/helpers/errors';
import { Errors } from 'src/helpers/errors';
import { LoggerService } from 'src/logger/logger.service';
import { TTeam } from 'src/team/types/team';
import { UserService } from 'src/user/services/user/user.service';
Expand Down Expand Up @@ -40,7 +40,7 @@ export class TeamService {
return team;
} catch (error) {
if (error instanceof NotFoundException) {
throw new NotFoundException(errors.teamDoesNotExist);
throw new NotFoundException(Errors.RESOURCE_NOT_FOUND, team_id);
}

this.logger.error(`some error ocurred : ${error.message}`);
Expand Down
4 changes: 2 additions & 2 deletions src/updates/services/updates/updates.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ProjectsService } from 'src/projects/services/projects/projects.service
import { TUpdate } from 'src/updates/types/updates';
import { UserService } from 'src/user/services/user/user.service';
import { Updates } from '@prisma/client';
import { errors } from 'src/helpers/errors';
import { Errors } from 'src/helpers/errors';
import { LoggerService } from 'src/logger/logger.service';

@Injectable()
Expand Down Expand Up @@ -46,7 +46,7 @@ export class UpdatesService {
return updateFound;
} catch (error) {
if (error instanceof NotFoundException) {
throw new NotFoundException(errors.updateDoesNotExist);
throw new NotFoundException(Errors.RESOURCE_NOT_FOUND, update_id);
}
this.logger.error(`some error ocurred : ${error.message}`);
throw error;
Expand Down
6 changes: 3 additions & 3 deletions src/user/services/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { PrismaService } from 'src/database/prisma.service';
import { TUser } from 'src/user/ultils/types';
import { User } from '@prisma/client';
import { SendEmailService } from 'src/send-email/service/send-email/send-email.service';
import { errors } from 'src/helpers/errors';
import { Errors } from 'src/helpers/errors';
import { HashService } from 'src/hash/service/hash/hash.service';
import { LoggerService } from 'src/logger/logger.service';

Expand Down Expand Up @@ -35,7 +35,7 @@ export class UserService {
return user;
} catch (error) {
if (error instanceof NotFoundException) {
throw new NotFoundException(errors.userDoesNotExist);
throw new NotFoundException(Errors.RESOURCE_NOT_FOUND, user_id);
}

throw new Error(
Expand All @@ -59,7 +59,7 @@ export class UserService {
return user;
} catch (error) {
if (error instanceof ConflictException) {
throw new ConflictException(errors.userAlreadyExist);
throw new ConflictException(Errors.RESOURCE_ALREADY_EXISTS, email);
}

this.logger.error(`some error ocurred : ${error.message}`);
Expand Down

0 comments on commit 3e80593

Please sign in to comment.