Skip to content

Commit 252c167

Browse files
authored
Merge pull request #22 from Visual-Regression-Tracker/73-register-error
73 register error
2 parents cc7cfdc + acbacd3 commit 252c167

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

src/app.module.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { TestRunsModule } from './test-runs/test-runs.module';
77
import { TestVariationsModule } from './test-variations/test-variations.module';
88
import { PrismaService } from './prisma/prisma.service';
99
import { ConfigModule } from '@nestjs/config';
10+
import { APP_FILTER } from '@nestjs/core';
11+
import { HttpPrismaExceptionFilter } from './http-prisma-exception.filter';
1012

1113
@Module({
1214
imports: [
@@ -18,6 +20,12 @@ import { ConfigModule } from '@nestjs/config';
1820
TestRunsModule,
1921
TestVariationsModule,
2022
],
21-
providers: [PrismaService],
23+
providers: [
24+
PrismaService,
25+
{
26+
provide: APP_FILTER,
27+
useClass: HttpPrismaExceptionFilter,
28+
},
29+
],
2230
})
2331
export class AppModule {}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { ExceptionFilter, Catch, ArgumentsHost, HttpException, HttpStatus } from '@nestjs/common';
2+
import { Request, Response } from 'express';
3+
import { PrismaClientKnownRequestError } from '@prisma/client';
4+
5+
@Catch(PrismaClientKnownRequestError)
6+
export class HttpPrismaExceptionFilter implements ExceptionFilter {
7+
catch(exception: PrismaClientKnownRequestError, host: ArgumentsHost) {
8+
const ctx = host.switchToHttp();
9+
const response = ctx.getResponse<Response>();
10+
const request = ctx.getRequest<Request>();
11+
12+
response.status(HttpStatus.INTERNAL_SERVER_ERROR).json({
13+
stack: exception.stack,
14+
message: exception.message,
15+
code: exception.code,
16+
timestamp: new Date().toISOString(),
17+
path: request.url,
18+
});
19+
}
20+
}

src/users/users.service.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,11 @@ export class UsersService {
2525
password: await this.authService.encryptPassword(createUserDto.password),
2626
};
2727

28-
try {
29-
const userData = await this.prismaService.user.create({
30-
data: user,
31-
});
32-
33-
return new UserLoginResponseDto(userData, null);
34-
} catch (err) {
35-
if (err.original.constraint === 'user_email_key') {
36-
throw new HttpException(`User with email '${err.errors[0].value}' already exists`, HttpStatus.CONFLICT);
37-
}
28+
const userData = await this.prismaService.user.create({
29+
data: user,
30+
});
3831

39-
throw new HttpException(err, HttpStatus.INTERNAL_SERVER_ERROR);
40-
}
32+
return new UserLoginResponseDto(userData, null);
4133
}
4234

4335
async findOne(id: string): Promise<User> {

0 commit comments

Comments
 (0)