Skip to content

Commit 6740851

Browse files
authored
Merge pull request #71 from GeneralMagicio/remove-handleError
remove handleError
2 parents dc5b42b + 79ed21b commit 6740851

File tree

5 files changed

+28
-95
lines changed

5 files changed

+28
-95
lines changed

src/auth/auth.controller.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
import { Body, Controller, Get, Post, Req, Res } from '@nestjs/common';
1+
import {
2+
BadRequestException,
3+
Body,
4+
Controller,
5+
Get,
6+
Post,
7+
Req,
8+
Res,
9+
} from '@nestjs/common';
210
import { MiniAppWalletAuthSuccessPayload } from '@worldcoin/minikit-js';
311
import { Request, Response } from 'express';
4-
import { handleError } from '../common/helpers';
512
import { AuthService } from './auth.service';
613

714
interface IRequestPayload {
@@ -34,32 +41,20 @@ export class AuthController {
3441
maxAge: 2 * 60 * 1000, //2 minutes
3542
});
3643

37-
return res.json({ nonce });
44+
return { nonce };
3845
}
3946

4047
@Post('verifyPayload')
4148
async verifyPayload(
4249
@Req() req: RequestWithCookies,
4350
@Body() body: IRequestPayload,
44-
@Res() res: Response,
4551
) {
4652
const { payload } = body;
4753
const storedNonce = req.cookies.siwe;
4854
if (!storedNonce) {
49-
return res.status(400).json({
50-
status: 'error',
51-
isValid: false,
52-
message: 'No nonce found in cookies',
53-
});
54-
}
55-
try {
56-
const validMessage = await this.authService.verifyPayload(
57-
payload,
58-
storedNonce,
59-
);
60-
return res.status(200).json({ isValid: validMessage });
61-
} catch (error: unknown) {
62-
return handleError(error);
55+
throw new BadRequestException('No nonce found in cookies');
6356
}
57+
const isValid = await this.authService.verifyPayload(payload, storedNonce);
58+
return { isValid };
6459
}
6560
}

src/common/exceptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class VoteNotFoundException extends HttpException {
2626

2727
export class DuplicateVoteException extends HttpException {
2828
constructor(message = 'User has already voted in this poll') {
29-
super(message, HttpStatus.CONFLICT);
29+
super(message, HttpStatus.BAD_REQUEST);
3030
}
3131
}
3232

src/common/helpers.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/poll/poll.controller.ts

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ import {
66
Param,
77
Post,
88
Query,
9-
Req,
10-
Res,
119
} from '@nestjs/common';
12-
import { Response } from 'express';
13-
import { handleError } from '../common/helpers';
1410
import { CreatePollDto, DeletePollDto, GetPollsDto } from './Poll.dto';
1511
import { PollService } from './poll.service';
1612

@@ -20,48 +16,22 @@ export class PollController {
2016

2117
@Post()
2218
async createPoll(@Body() dto: CreatePollDto) {
23-
try {
24-
return await this.pollService.createPoll(dto);
25-
} catch (error: unknown) {
26-
return handleError(error);
27-
}
19+
return await this.pollService.createPoll(dto);
2820
}
2921

3022
@Get()
31-
async getPolls(
32-
@Req() req,
33-
@Query() query: GetPollsDto,
34-
@Res() res: Response,
35-
) {
36-
try {
37-
const polls = await this.pollService.getPolls(query);
38-
return res.status(200).json(polls);
39-
} catch (error: unknown) {
40-
return handleError(error);
41-
}
23+
async getPolls(@Query() query: GetPollsDto) {
24+
return await this.pollService.getPolls(query);
4225
}
4326

4427
@Get(':id')
45-
async getPollDetails(@Param('id') id: number, @Res() res: Response) {
46-
try {
47-
const poll = await this.pollService.getPollDetails(Number(id));
48-
return res.status(200).json(poll);
49-
} catch (error: unknown) {
50-
return handleError(error);
51-
}
28+
async getPollDetails(@Param('id') id: number) {
29+
return await this.pollService.getPollDetails(Number(id));
5230
}
5331

5432
@Delete(':id')
55-
async deletePoll(
56-
@Param('id') id: number,
57-
@Body() query: DeletePollDto,
58-
@Res() res: Response,
59-
) {
60-
try {
61-
const poll = await this.pollService.deletePoll(Number(id), query);
62-
return res.status(200).json({ message: 'Poll deleted', poll: poll });
63-
} catch (error: unknown) {
64-
return handleError(error);
65-
}
33+
async deletePoll(@Param('id') id: number, @Body() query: DeletePollDto) {
34+
const poll = await this.pollService.deletePoll(Number(id), query);
35+
return { message: 'Poll deleted', poll };
6636
}
6737
}

src/user/user.controller.ts

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Body, Controller, Get, Post, Query } from '@nestjs/common';
2-
import { handleError } from '../common/helpers';
32
import {
43
CreateUserDto,
54
CreateUserResponseDto,
@@ -24,59 +23,35 @@ export class UserController {
2423
async getUserData(
2524
@Query() query: GetUserDataDto,
2625
): Promise<UserDataResponseDto> {
27-
try {
28-
return await this.userService.getUserData(query);
29-
} catch (error: unknown) {
30-
return handleError(error);
31-
}
26+
return await this.userService.getUserData(query);
3227
}
3328

3429
@Get('getUserActivities')
3530
async getUserActivities(
3631
@Query() query: GetUserActivitiesDto,
3732
): Promise<UserActivitiesResponseDto> {
38-
try {
39-
return await this.userService.getUserActivities(query);
40-
} catch (error: unknown) {
41-
return handleError(error);
42-
}
33+
return await this.userService.getUserActivities(query);
4334
}
4435

4536
@Get('getUserVotes')
4637
async getUserVotes(
4738
@Query() query: GetUserVotesDto,
4839
): Promise<UserVotesResponseDto> {
49-
try {
50-
return await this.userService.getUserVotes(query);
51-
} catch (error: unknown) {
52-
return handleError(error);
53-
}
40+
return await this.userService.getUserVotes(query);
5441
}
5542

5643
@Post('setVote')
5744
async setVote(@Body() dto: SetVoteDto): Promise<SetVoteResponseDto> {
58-
try {
59-
return await this.userService.setVote(dto);
60-
} catch (error: unknown) {
61-
return handleError(error);
62-
}
45+
return await this.userService.setVote(dto);
6346
}
6447

6548
@Post('editVote')
6649
async editVote(@Body() dto: EditVoteDto): Promise<EditVoteResponseDto> {
67-
try {
68-
return await this.userService.editVote(dto);
69-
} catch (error: unknown) {
70-
return handleError(error);
71-
}
50+
return await this.userService.editVote(dto);
7251
}
7352

7453
@Post('createUser')
7554
async createUser(@Body() dto: CreateUserDto): Promise<CreateUserResponseDto> {
76-
try {
77-
return await this.userService.createUser(dto);
78-
} catch (error: unknown) {
79-
return handleError(error);
80-
}
55+
return await this.userService.createUser(dto);
8156
}
8257
}

0 commit comments

Comments
 (0)