Skip to content

Commit

Permalink
fix: pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
MatanYadaev committed Mar 11, 2024
1 parent 9d62068 commit ee96e4a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ArgumentsHost, Catch, HttpException, HttpStatus } from '@nestjs/common';
import { BaseExceptionFilter, HttpAdapterHost } from '@nestjs/core';
import { Prisma } from '@prisma/client';
import { PRISMA_UNIQUE_CONSTRAINT_ERROR } from '@/prisma/prisma.util';

export type ErrorCodesStatusMapping = {
[key: string]: number;
Expand Down Expand Up @@ -43,8 +44,7 @@ export class HttpExceptionFilter extends BaseExceptionFilter {
if (host.getType() === 'http') {
// for http requests (REST)
// Todo : Add all other exception types and also add mapping
if (exception.code === 'P2002') {
// Handling Unique Key Constraint Violation Error
if (exception.code === PRISMA_UNIQUE_CONSTRAINT_ERROR) {
const fields = (exception.meta as { target: string[] }).target;
message = `Another record with the requested (${fields.join(', ')}) already exists`;
} else {
Expand Down
1 change: 1 addition & 0 deletions services/workflows-service/src/prisma/prisma.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Prisma } from '@prisma/client';

export const PRISMA_RECORD_NOT_FOUND_ERROR = 'P2025';
export const PRISMA_FOREIGN_KEY_CONSTRAINT_ERROR = 'P2003';
export const PRISMA_UNIQUE_CONSTRAINT_ERROR = 'P2002';

export const isRecordNotFoundError = (
error: unknown,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { TProjectId } from '@/types';
import { TransactionCreatedDto } from '@/transaction/dtos/transaction-created.dto';
import { Prisma } from '@prisma/client';
import { SentryService } from '@/sentry/sentry.service';
import { PRISMA_UNIQUE_CONSTRAINT_ERROR } from '@/prisma/prisma.util';

@Injectable()
export class TransactionService {
Expand Down Expand Up @@ -44,7 +45,10 @@ export class TransactionService {
} catch (error) {
let errorToLog: Error = new Error('Unknown error', { cause: error });

if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === 'P2002') {
if (
error instanceof Prisma.PrismaClientKnownRequestError &&
error.code === PRISMA_UNIQUE_CONSTRAINT_ERROR
) {
errorToLog = new Error('Transaction already exists', { cause: error });
} else {
this.sentry.captureException(errorToLog);
Expand Down

0 comments on commit ee96e4a

Please sign in to comment.