Skip to content

Commit 99e4ba1

Browse files
committed
fix: 500 error. When the movie is not found
#38
1 parent e7df343 commit 99e4ba1

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/common/base/base.controller.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { ApiOperation, ApiResponse } from '@nestjs/swagger';
2-
import { Get, Param, Query } from '@nestjs/common';
1+
import { ApiNotFoundResponse, ApiOperation, ApiResponse } from '@nestjs/swagger';
2+
import { Get, NotFoundException, Param, Query } from '@nestjs/common';
33
import { IQuery } from '../interfaces/query.interface';
44
import { Paginated } from '../decorators/paginated.decorator';
55
import { ApiBaseResponse } from '../decorators/api-base-response.decorator';
6+
import { ForbiddenErrorResponseDto } from '../dto/errors/forbidden-error.response.dto';
67

78
type Constructor<T> = new (...args: any[]) => T;
89

@@ -44,8 +45,12 @@ export function BaseControllerWithFindById<TEntity, TEntityDto>(
4445
@Get(':id')
4546
@ApiOperation({ summary: 'Поиск по id' })
4647
@ApiBaseResponse({ type: Entity })
48+
@ApiNotFoundResponse({ type: ForbiddenErrorResponseDto, description: 'NotFound' })
4749
async findOne(@Param('id') id: string): Promise<TEntity> {
48-
return this.service.findOne(+id);
50+
const found = await this.service.findOne(+id);
51+
if (!found) throw new NotFoundException('По этому id ничего не найдено!');
52+
53+
return found.toJSON();
4954
}
5055
}
5156

src/common/base/base.service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ export abstract class BaseService<T> implements IBaseService<T> {
4444
async findOne(id: number | string): Promise<T | null> {
4545
const found = await this.model.findOne({ id });
4646
// @ts-ignore
47-
return found.toJSON();
47+
return found;
4848
}
4949
}

0 commit comments

Comments
 (0)