Skip to content

Commit c02fd92

Browse files
authored
Merge pull request #19 from FarmLink-ppp/exceptions-filter
fix: fix error handling
2 parents 67c40f4 + 326cb7c commit c02fd92

File tree

2 files changed

+34
-62
lines changed

2 files changed

+34
-62
lines changed

src/auth/auth.service.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
ForbiddenException,
55
Injectable,
66
InternalServerErrorException,
7-
NotFoundException,
87
UnauthorizedException,
98
} from '@nestjs/common';
109
import { Response } from 'express';
@@ -194,10 +193,7 @@ export class AuthService {
194193
'If this email is registered and not verified, a verification email has been sent. Please check your inbox or spam folder.',
195194
};
196195
} catch (error) {
197-
if (
198-
error instanceof NotFoundException ||
199-
error instanceof BadRequestException
200-
) {
196+
if (error instanceof BadRequestException) {
201197
throw error;
202198
}
203199
throw new InternalServerErrorException(

src/farm/farm.service.ts

Lines changed: 33 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
Injectable,
33
NotFoundException,
44
ForbiddenException,
5-
InternalServerErrorException,
65
} from '@nestjs/common';
76
import { PrismaService } from './../prisma/prisma.service';
87
import { CreateFarmDto } from './dto/create-farm.dto';
@@ -12,73 +11,50 @@ export class FarmService {
1211
constructor(private prisma: PrismaService) {}
1312

1413
async createFarm(createFarmDto: CreateFarmDto, userId: number) {
15-
try {
16-
const existingFarm = await this.prisma.farm.findUnique({
17-
where: { user_id: userId },
18-
});
14+
const existingFarm = await this.prisma.farm.findUnique({
15+
where: { user_id: userId },
16+
});
1917

20-
if (existingFarm) {
21-
throw new ForbiddenException('User already has a farm');
22-
}
23-
return await this.prisma.farm.create({
24-
data: {
25-
name: createFarmDto.name,
26-
location: createFarmDto.location,
27-
area_unit: createFarmDto.areaUnit,
28-
total_area: createFarmDto.totalArea,
29-
user: { connect: { id: userId } },
30-
},
31-
});
32-
} catch (error) {
33-
if (error instanceof ForbiddenException) throw error;
34-
throw new InternalServerErrorException(error, 'Error creating farm');
18+
if (existingFarm) {
19+
throw new ForbiddenException('User already has a farm');
3520
}
21+
return await this.prisma.farm.create({
22+
data: {
23+
name: createFarmDto.name,
24+
location: createFarmDto.location,
25+
area_unit: createFarmDto.areaUnit,
26+
total_area: createFarmDto.totalArea,
27+
user: { connect: { id: userId } },
28+
},
29+
});
3630
}
3731

3832
async getFarmByUserId(userId: number) {
39-
try {
40-
const farm = await this.prisma.farm.findUnique({
41-
where: { user_id: userId },
42-
});
43-
if (!farm) throw new NotFoundException('Farm not found');
44-
return farm;
45-
} catch (error) {
46-
if (error instanceof NotFoundException) throw error;
47-
throw new InternalServerErrorException(
48-
error,
49-
'Error fetching farm by user ID',
50-
);
51-
}
33+
const farm = await this.prisma.farm.findUnique({
34+
where: { user_id: userId },
35+
});
36+
if (!farm) throw new NotFoundException('Farm not found');
37+
return farm;
5238
}
5339

5440
async update(updateFarmDto: UpdateFarmDto, userId: number) {
55-
try {
56-
const farm = await this.getFarmByUserId(userId);
41+
const farm = await this.getFarmByUserId(userId);
5742

58-
return await this.prisma.farm.update({
59-
where: { user_id: userId },
60-
data: {
61-
name: updateFarmDto.name ?? farm.name,
62-
total_area: updateFarmDto.totalArea ?? farm.total_area,
63-
location: updateFarmDto.location ?? farm.location,
64-
area_unit: updateFarmDto.areaUnit ?? farm.area_unit,
65-
},
66-
});
67-
} catch (error) {
68-
if (error instanceof NotFoundException) throw error;
69-
throw new InternalServerErrorException(error, 'Error updating farm');
70-
}
43+
return await this.prisma.farm.update({
44+
where: { user_id: userId },
45+
data: {
46+
name: updateFarmDto.name ?? farm.name,
47+
total_area: updateFarmDto.totalArea ?? farm.total_area,
48+
location: updateFarmDto.location ?? farm.location,
49+
area_unit: updateFarmDto.areaUnit ?? farm.area_unit,
50+
},
51+
});
7152
}
7253

7354
async remove(userId: number) {
74-
try {
75-
await this.getFarmByUserId(userId);
76-
return await this.prisma.farm.delete({
77-
where: { user_id: userId },
78-
});
79-
} catch (error) {
80-
if (error instanceof NotFoundException) throw error;
81-
throw new InternalServerErrorException(error, 'Error deleting farm');
82-
}
55+
await this.getFarmByUserId(userId);
56+
return await this.prisma.farm.delete({
57+
where: { user_id: userId },
58+
});
8359
}
8460
}

0 commit comments

Comments
 (0)