diff --git a/src/user/user.controller.ts b/src/user/user.controller.ts index 6ca7e54..65fb111 100644 --- a/src/user/user.controller.ts +++ b/src/user/user.controller.ts @@ -1,4 +1,11 @@ -import { Body, Controller, Get, Post, Query } from '@nestjs/common'; +import { + BadRequestException, + Body, + Controller, + Get, + Post, + Query, +} from '@nestjs/common'; import { UserService } from './user.service'; import { EditVoteDto, @@ -21,30 +28,60 @@ export class UserController { async getUserData( @Query() query: GetUserDataDto, ): Promise { - return this.userService.getUserData(query); + try { + return this.userService.getUserData(query); + } catch (error: unknown) { + const errorMessage = + error instanceof Error ? error.message : 'An unexpected error occurred'; + throw new BadRequestException(errorMessage); + } } @Get('getUserActivities') async getUserActivities( @Query() query: GetUserActivitiesDto, ): Promise { - return this.userService.getUserActivities(query); + try { + return this.userService.getUserActivities(query); + } catch (error: unknown) { + const errorMessage = + error instanceof Error ? error.message : 'An unexpected error occurred'; + throw new BadRequestException(errorMessage); + } } @Post('getUserVotes') async getUserVotes( @Body() body: GetUserVotesDto, ): Promise { - return this.userService.getUserVotes(body); + try { + return await this.userService.getUserVotes(body); + } catch (error: unknown) { + const errorMessage = + error instanceof Error ? error.message : 'An unexpected error occurred'; + throw new BadRequestException(errorMessage); + } } @Post('setVote') async setVote(@Body() dto: SetVoteDto): Promise { - return this.userService.setVote(dto); + try { + return this.userService.setVote(dto); + } catch (error: unknown) { + const errorMessage = + error instanceof Error ? error.message : 'An unexpected error occurred'; + throw new BadRequestException(errorMessage); + } } @Post('editVote') async editVote(@Body() dto: EditVoteDto): Promise { - return this.userService.editVote(dto); + try { + return this.userService.editVote(dto); + } catch (error: unknown) { + const errorMessage = + error instanceof Error ? error.message : 'An unexpected error occurred'; + throw new BadRequestException(errorMessage); + } } } diff --git a/src/user/user.service.ts b/src/user/user.service.ts index 88656c5..ef681e4 100644 --- a/src/user/user.service.ts +++ b/src/user/user.service.ts @@ -117,7 +117,7 @@ export class UserService { select: { endDate: true, options: true }, }); if (!poll || poll.endDate < new Date()) { - throw new Error('Poll is not active or does not exist'); + throw new Error('Poll is not active or does not exist!'); } const votes = await this.databaseService.vote.findMany({ where: {