Skip to content

Commit

Permalink
fix(): fix QueryFailedFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
NarHakobyan committed Apr 1, 2024
1 parent 633f97b commit d76512e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 39 deletions.
67 changes: 30 additions & 37 deletions src/filters/query-failed.filter.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,30 @@
// import { STATUS_CODES } from 'node:http';
//
// import {
// type ArgumentsHost,
// Catch,
// type ExceptionFilter,
// HttpStatus,
// } from '@nestjs/common';
// import { Reflector } from '@nestjs/core';
// import { type Response } from 'express';
//
// import { constraintErrors } from './constraint-errors';
//
// @Catch(QueryFailedError)
// export class QueryFailedFilter implements ExceptionFilter<QueryFailedError> {
// constructor(public reflector: Reflector) {}
//
// catch(
// exception: QueryFailedError & { constraint?: string },
// host: ArgumentsHost,
// ) {
// const ctx = host.switchToHttp();
// const response = ctx.getResponse<Response>();
//
// const status = exception.constraint?.startsWith('UQ')
// ? HttpStatus.CONFLICT
// : HttpStatus.INTERNAL_SERVER_ERROR;
//
// response.status(status).json({
// statusCode: status,
// error: STATUS_CODES[status],
// message: exception.constraint
// ? constraintErrors[exception.constraint]
// : undefined,
// });
// }
// }
import { STATUS_CODES } from 'node:http';

import { UniqueConstraintViolationException } from '@mikro-orm/core';
import type { ArgumentsHost, ExceptionFilter } from '@nestjs/common';
import { Catch, HttpStatus } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import type { Response } from 'express';

@Catch(UniqueConstraintViolationException)
export class QueryFailedFilter
implements ExceptionFilter<UniqueConstraintViolationException>
{
constructor(public reflector: Reflector) {}

catch(
exception: UniqueConstraintViolationException & { constraint: string },
host: ArgumentsHost,
) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();

const status = HttpStatus.CONFLICT;

response.status(status).json({
statusCode: status,
error: STATUS_CODES[status],
message: `error.${exception.constraint}`,
});
}
}
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { setupSwagger } from './setup-swagger';
import { ApiConfigService } from './shared/services/api-config.service';
import { TranslationService } from './shared/services/translation.service';
import { SharedModule } from './shared/shared.module';
import { QueryFailedFilter } from './filters/query-failed.filter.ts';

export async function bootstrap(): Promise<NestExpressApplication> {
const app = await NestFactory.create<NestExpressApplication>(
Expand All @@ -38,7 +39,7 @@ export async function bootstrap(): Promise<NestExpressApplication> {

app.useGlobalFilters(
new HttpExceptionFilter(reflector),
// new QueryFailedFilter(reflector),
new QueryFailedFilter(reflector),
);

app.useGlobalInterceptors(
Expand Down
3 changes: 2 additions & 1 deletion src/modules/post/post-translation.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export class PostTranslationEntity extends AbstractTranslationEntity<PostTransla
@Property({ type: 'uuid', fieldName: 'post_id', persist: false })
postId!: string; // Ensure this is your foreign key column, linked via the ManyToOne relation below

@ManyToOne(() => PostEntity, {
@ManyToOne({
entity: () => PostEntity, // This tells MikroORM the entity to use for the relation
fieldName: 'post_id', // This tells MikroORM the column name to use, which should match the one defined in @Property above
joinColumn: 'post_id', // Explicitly define the join column name for clarity, though this is optional if fieldName is already specified
columnType: 'uuid', // This specifies the column type, ensuring it matches the type defined in @Property
Expand Down

0 comments on commit d76512e

Please sign in to comment.