Skip to content

Commit 03765e7

Browse files
committed
add error handling to user endpoints
1 parent b5866a3 commit 03765e7

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

src/user/user.controller.ts

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { Body, Controller, Get, Post, Query } from '@nestjs/common';
1+
import {
2+
BadRequestException,
3+
Body,
4+
Controller,
5+
Get,
6+
Post,
7+
Query,
8+
} from '@nestjs/common';
29
import { UserService } from './user.service';
310
import {
411
EditVoteDto,
@@ -21,30 +28,60 @@ export class UserController {
2128
async getUserData(
2229
@Query() query: GetUserDataDto,
2330
): Promise<UserDataResponseDto> {
24-
return this.userService.getUserData(query);
31+
try {
32+
return this.userService.getUserData(query);
33+
} catch (error: unknown) {
34+
const errorMessage =
35+
error instanceof Error ? error.message : 'An unexpected error occurred';
36+
throw new BadRequestException(errorMessage);
37+
}
2538
}
2639

2740
@Get('getUserActivities')
2841
async getUserActivities(
2942
@Query() query: GetUserActivitiesDto,
3043
): Promise<UserActivitiesResponseDto> {
31-
return this.userService.getUserActivities(query);
44+
try {
45+
return this.userService.getUserActivities(query);
46+
} catch (error: unknown) {
47+
const errorMessage =
48+
error instanceof Error ? error.message : 'An unexpected error occurred';
49+
throw new BadRequestException(errorMessage);
50+
}
3251
}
3352

3453
@Post('getUserVotes')
3554
async getUserVotes(
3655
@Body() body: GetUserVotesDto,
3756
): Promise<UserVotesResponseDto> {
38-
return this.userService.getUserVotes(body);
57+
try {
58+
return await this.userService.getUserVotes(body);
59+
} catch (error: unknown) {
60+
const errorMessage =
61+
error instanceof Error ? error.message : 'An unexpected error occurred';
62+
throw new BadRequestException(errorMessage);
63+
}
3964
}
4065

4166
@Post('setVote')
4267
async setVote(@Body() dto: SetVoteDto): Promise<SetVoteResponseDto> {
43-
return this.userService.setVote(dto);
68+
try {
69+
return this.userService.setVote(dto);
70+
} catch (error: unknown) {
71+
const errorMessage =
72+
error instanceof Error ? error.message : 'An unexpected error occurred';
73+
throw new BadRequestException(errorMessage);
74+
}
4475
}
4576

4677
@Post('editVote')
4778
async editVote(@Body() dto: EditVoteDto): Promise<EditVoteResponseDto> {
48-
return this.userService.editVote(dto);
79+
try {
80+
return this.userService.editVote(dto);
81+
} catch (error: unknown) {
82+
const errorMessage =
83+
error instanceof Error ? error.message : 'An unexpected error occurred';
84+
throw new BadRequestException(errorMessage);
85+
}
4986
}
5087
}

src/user/user.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export class UserService {
117117
select: { endDate: true, options: true },
118118
});
119119
if (!poll || poll.endDate < new Date()) {
120-
throw new Error('Poll is not active or does not exist');
120+
throw new Error('Poll is not active or does not exist!');
121121
}
122122
const votes = await this.databaseService.vote.findMany({
123123
where: {

0 commit comments

Comments
 (0)