Refactor error handling in controllers and services

This commit is contained in:
faris Aljohari
2024-04-14 11:28:52 +03:00
parent 53628236a6
commit d92ae03eac
10 changed files with 287 additions and 308 deletions

View File

@ -35,7 +35,7 @@ export class BuildingController {
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
@ -49,16 +49,12 @@ export class BuildingController {
await this.buildingService.getBuildingByUuid(buildingUuid); await this.buildingService.getBuildingByUuid(buildingUuid);
return building; return building;
} catch (error) { } catch (error) {
if (error.status === 404) {
throw new HttpException('Building not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
}
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -74,16 +70,12 @@ export class BuildingController {
); );
return building; return building;
} catch (error) { } catch (error) {
if (error.status === 404) {
throw new HttpException('Building not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
}
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get('parent/:buildingUuid') @Get('parent/:buildingUuid')
@ -93,16 +85,12 @@ export class BuildingController {
await this.buildingService.getBuildingParentByUuid(buildingUuid); await this.buildingService.getBuildingParentByUuid(buildingUuid);
return building; return building;
} catch (error) { } catch (error) {
if (error.status === 404) {
throw new HttpException('Building not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
}
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Put('rename/:buildingUuid') @Put('rename/:buildingUuid')
@ -117,9 +105,6 @@ export class BuildingController {
); );
return building; return building;
} catch (error) { } catch (error) {
if (error.status === 404) {
throw new HttpException('Building not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
@ -127,4 +112,3 @@ export class BuildingController {
} }
} }
} }
}

View File

@ -55,8 +55,12 @@ export class BuildingService {
}, },
relations: ['spaceType'], relations: ['spaceType'],
}); });
if (!building) { if (
throw new HttpException('Building not found', HttpStatus.NOT_FOUND); !building ||
!building.spaceType ||
building.spaceType.type !== 'building'
) {
throw new BadRequestException('Invalid building UUID');
} }
return { return {
uuid: building.uuid, uuid: building.uuid,
@ -66,25 +70,26 @@ export class BuildingService {
type: building.spaceType.type, type: building.spaceType.type,
}; };
} catch (err) { } catch (err) {
throw new HttpException( if (err instanceof BadRequestException) {
err.message, throw err; // Re-throw BadRequestException
err.status || HttpStatus.INTERNAL_SERVER_ERROR, } else {
); throw new HttpException('Building not found', HttpStatus.NOT_FOUND);
}
} }
} }
async getBuildingChildByUuid( async getBuildingChildByUuid(
buildingUuid: string, buildingUuid: string,
getBuildingChildDto: GetBuildingChildDto, getBuildingChildDto: GetBuildingChildDto,
): Promise<BuildingChildInterface> { ): Promise<BuildingChildInterface> {
try {
const { includeSubSpaces, page, pageSize } = getBuildingChildDto; const { includeSubSpaces, page, pageSize } = getBuildingChildDto;
const space = await this.spaceRepository.findOneOrFail({ const space = await this.spaceRepository.findOneOrFail({
where: { uuid: buildingUuid }, where: { uuid: buildingUuid },
relations: ['children', 'spaceType'], relations: ['children', 'spaceType'],
}); });
if (!space || !space.spaceType || space.spaceType.type !== 'building') {
if (space.spaceType.type !== 'building') { throw new BadRequestException('Invalid building UUID');
throw new HttpException('Building not found', HttpStatus.NOT_FOUND);
} }
const totalCount = await this.spaceRepository.count({ const totalCount = await this.spaceRepository.count({
@ -105,6 +110,13 @@ export class BuildingService {
totalCount, totalCount,
children, children,
}; };
} catch (err) {
if (err instanceof BadRequestException) {
throw err; // Re-throw BadRequestException
} else {
throw new HttpException('Building not found', HttpStatus.NOT_FOUND);
}
}
} }
private async buildHierarchy( private async buildHierarchy(
@ -165,10 +177,13 @@ export class BuildingService {
}, },
relations: ['spaceType', 'parent', 'parent.spaceType'], relations: ['spaceType', 'parent', 'parent.spaceType'],
}); });
if (!building) { if (
throw new HttpException('Building not found', HttpStatus.NOT_FOUND); !building ||
!building.spaceType ||
building.spaceType.type !== 'building'
) {
throw new BadRequestException('Invalid building UUID');
} }
return { return {
uuid: building.uuid, uuid: building.uuid,
name: building.spaceName, name: building.spaceName,
@ -180,10 +195,11 @@ export class BuildingService {
}, },
}; };
} catch (err) { } catch (err) {
throw new HttpException( if (err instanceof BadRequestException) {
err.message, throw err; // Re-throw BadRequestException
err.status || HttpStatus.INTERNAL_SERVER_ERROR, } else {
); throw new HttpException('Building not found', HttpStatus.NOT_FOUND);
}
} }
} }

View File

@ -35,7 +35,7 @@ export class CommunityController {
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
@ -49,16 +49,12 @@ export class CommunityController {
await this.communityService.getCommunityByUuid(communityUuid); await this.communityService.getCommunityByUuid(communityUuid);
return community; return community;
} catch (error) { } catch (error) {
if (error.status === 404) {
throw new HttpException('Community not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
}
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -74,16 +70,12 @@ export class CommunityController {
); );
return community; return community;
} catch (error) { } catch (error) {
if (error.status === 404) {
throw new HttpException('Community not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
}
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -99,9 +91,6 @@ export class CommunityController {
); );
return community; return community;
} catch (error) { } catch (error) {
if (error.status === 404) {
throw new HttpException('Community not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
@ -109,4 +98,3 @@ export class CommunityController {
} }
} }
} }
}

View File

@ -53,8 +53,12 @@ export class CommunityService {
}, },
relations: ['spaceType'], relations: ['spaceType'],
}); });
if (!community) { if (
throw new HttpException('Community not found', HttpStatus.NOT_FOUND); !community ||
!community.spaceType ||
community.spaceType.type !== 'community'
) {
throw new BadRequestException('Invalid community UUID');
} }
return { return {
uuid: community.uuid, uuid: community.uuid,
@ -64,16 +68,18 @@ export class CommunityService {
type: community.spaceType.type, type: community.spaceType.type,
}; };
} catch (err) { } catch (err) {
throw new HttpException( if (err instanceof BadRequestException) {
err.message, throw err; // Re-throw BadRequestException
err.status || HttpStatus.INTERNAL_SERVER_ERROR, } else {
); throw new HttpException('Community not found', HttpStatus.NOT_FOUND);
}
} }
} }
async getCommunityChildByUuid( async getCommunityChildByUuid(
communityUuid: string, communityUuid: string,
getCommunityChildDto: GetCommunityChildDto, getCommunityChildDto: GetCommunityChildDto,
): Promise<CommunityChildInterface> { ): Promise<CommunityChildInterface> {
try {
const { includeSubSpaces, page, pageSize } = getCommunityChildDto; const { includeSubSpaces, page, pageSize } = getCommunityChildDto;
const space = await this.spaceRepository.findOneOrFail({ const space = await this.spaceRepository.findOneOrFail({
@ -81,8 +87,8 @@ export class CommunityService {
relations: ['children', 'spaceType'], relations: ['children', 'spaceType'],
}); });
if (space.spaceType.type !== 'community') { if (!space || !space.spaceType || space.spaceType.type !== 'community') {
throw new HttpException('Community not found', HttpStatus.NOT_FOUND); throw new BadRequestException('Invalid community UUID');
} }
const totalCount = await this.spaceRepository.count({ const totalCount = await this.spaceRepository.count({
where: { parent: { uuid: space.uuid } }, where: { parent: { uuid: space.uuid } },
@ -100,6 +106,13 @@ export class CommunityService {
totalCount, totalCount,
children, children,
}; };
} catch (err) {
if (err instanceof BadRequestException) {
throw err; // Re-throw BadRequestException
} else {
throw new HttpException('Community not found', HttpStatus.NOT_FOUND);
}
}
} }
private async buildHierarchy( private async buildHierarchy(

View File

@ -25,8 +25,8 @@ import { UpdateFloorNameDto } from '../dtos/update.floor.dto';
export class FloorController { export class FloorController {
constructor(private readonly floorService: FloorService) {} constructor(private readonly floorService: FloorService) {}
// @ApiBearerAuth() @ApiBearerAuth()
// @UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Post() @Post()
async addFloor(@Body() addFloorDto: AddFloorDto) { async addFloor(@Body() addFloorDto: AddFloorDto) {
try { try {
@ -35,7 +35,7 @@ export class FloorController {
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
@ -48,16 +48,12 @@ export class FloorController {
const floor = await this.floorService.getFloorByUuid(floorUuid); const floor = await this.floorService.getFloorByUuid(floorUuid);
return floor; return floor;
} catch (error) { } catch (error) {
if (error.status === 404) {
throw new HttpException('Floor not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
}
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -73,16 +69,12 @@ export class FloorController {
); );
return floor; return floor;
} catch (error) { } catch (error) {
if (error.status === 404) {
throw new HttpException('Floor not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
}
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get('parent/:floorUuid') @Get('parent/:floorUuid')
@ -91,16 +83,12 @@ export class FloorController {
const floor = await this.floorService.getFloorParentByUuid(floorUuid); const floor = await this.floorService.getFloorParentByUuid(floorUuid);
return floor; return floor;
} catch (error) { } catch (error) {
if (error.status === 404) {
throw new HttpException('Floor not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
}
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -116,9 +104,6 @@ export class FloorController {
); );
return floor; return floor;
} catch (error) { } catch (error) {
if (error.status === 404) {
throw new HttpException('Floor not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
@ -126,4 +111,3 @@ export class FloorController {
} }
} }
} }
}

View File

@ -53,9 +53,10 @@ export class FloorService {
}, },
relations: ['spaceType'], relations: ['spaceType'],
}); });
if (!floor) { if (!floor || !floor.spaceType || floor.spaceType.type !== 'floor') {
throw new HttpException('Floor not found', HttpStatus.NOT_FOUND); throw new BadRequestException('Invalid floor UUID');
} }
return { return {
uuid: floor.uuid, uuid: floor.uuid,
createdAt: floor.createdAt, createdAt: floor.createdAt,
@ -64,16 +65,18 @@ export class FloorService {
type: floor.spaceType.type, type: floor.spaceType.type,
}; };
} catch (err) { } catch (err) {
throw new HttpException( if (err instanceof BadRequestException) {
err.message, throw err; // Re-throw BadRequestException
err.status || HttpStatus.INTERNAL_SERVER_ERROR, } else {
); throw new HttpException('Floor not found', HttpStatus.NOT_FOUND);
}
} }
} }
async getFloorChildByUuid( async getFloorChildByUuid(
floorUuid: string, floorUuid: string,
getFloorChildDto: GetFloorChildDto, getFloorChildDto: GetFloorChildDto,
): Promise<FloorChildInterface> { ): Promise<FloorChildInterface> {
try {
const { includeSubSpaces, page, pageSize } = getFloorChildDto; const { includeSubSpaces, page, pageSize } = getFloorChildDto;
const space = await this.spaceRepository.findOneOrFail({ const space = await this.spaceRepository.findOneOrFail({
@ -81,10 +84,9 @@ export class FloorService {
relations: ['children', 'spaceType'], relations: ['children', 'spaceType'],
}); });
if (space.spaceType.type !== 'floor') { if (!space || !space.spaceType || space.spaceType.type !== 'floor') {
throw new HttpException('Floor not found', HttpStatus.NOT_FOUND); throw new BadRequestException('Invalid floor UUID');
} }
const totalCount = await this.spaceRepository.count({ const totalCount = await this.spaceRepository.count({
where: { parent: { uuid: space.uuid } }, where: { parent: { uuid: space.uuid } },
}); });
@ -103,6 +105,13 @@ export class FloorService {
totalCount, totalCount,
children, children,
}; };
} catch (err) {
if (err instanceof BadRequestException) {
throw err; // Re-throw BadRequestException
} else {
throw new HttpException('Floor not found', HttpStatus.NOT_FOUND);
}
}
} }
private async buildHierarchy( private async buildHierarchy(
@ -163,8 +172,8 @@ export class FloorService {
}, },
relations: ['spaceType', 'parent', 'parent.spaceType'], relations: ['spaceType', 'parent', 'parent.spaceType'],
}); });
if (!floor) { if (!floor || !floor.spaceType || floor.spaceType.type !== 'floor') {
throw new HttpException('Floor not found', HttpStatus.NOT_FOUND); throw new BadRequestException('Invalid floor UUID');
} }
return { return {
@ -178,10 +187,11 @@ export class FloorService {
}, },
}; };
} catch (err) { } catch (err) {
throw new HttpException( if (err instanceof BadRequestException) {
err.message, throw err; // Re-throw BadRequestException
err.status || HttpStatus.INTERNAL_SERVER_ERROR, } else {
); throw new HttpException('Floor not found', HttpStatus.NOT_FOUND);
}
} }
} }

View File

@ -33,7 +33,7 @@ export class RoomController {
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
@ -46,16 +46,12 @@ export class RoomController {
const room = await this.roomService.getRoomByUuid(roomUuid); const room = await this.roomService.getRoomByUuid(roomUuid);
return room; return room;
} catch (error) { } catch (error) {
if (error.status === 404) {
throw new HttpException('Room not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
}
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -65,16 +61,12 @@ export class RoomController {
const room = await this.roomService.getRoomParentByUuid(roomUuid); const room = await this.roomService.getRoomParentByUuid(roomUuid);
return room; return room;
} catch (error) { } catch (error) {
if (error.status === 404) {
throw new HttpException('Room not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
}
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -90,9 +82,6 @@ export class RoomController {
); );
return room; return room;
} catch (error) { } catch (error) {
if (error.status === 404) {
throw new HttpException('Room not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
@ -100,4 +89,3 @@ export class RoomController {
} }
} }
} }
}

View File

@ -50,9 +50,10 @@ export class RoomService {
}, },
relations: ['spaceType'], relations: ['spaceType'],
}); });
if (!room) { if (!room || !room.spaceType || room.spaceType.type !== 'room') {
throw new HttpException('Room not found', HttpStatus.NOT_FOUND); throw new BadRequestException('Invalid room UUID');
} }
return { return {
uuid: room.uuid, uuid: room.uuid,
createdAt: room.createdAt, createdAt: room.createdAt,
@ -61,10 +62,11 @@ export class RoomService {
type: room.spaceType.type, type: room.spaceType.type,
}; };
} catch (err) { } catch (err) {
throw new HttpException( if (err instanceof BadRequestException) {
err.message, throw err; // Re-throw BadRequestException
err.status || HttpStatus.INTERNAL_SERVER_ERROR, } else {
); throw new HttpException('Room not found', HttpStatus.NOT_FOUND);
}
} }
} }
@ -79,8 +81,8 @@ export class RoomService {
}, },
relations: ['spaceType', 'parent', 'parent.spaceType'], relations: ['spaceType', 'parent', 'parent.spaceType'],
}); });
if (!room) { if (!room || !room.spaceType || room.spaceType.type !== 'room') {
throw new HttpException('Room not found', HttpStatus.NOT_FOUND); throw new BadRequestException('Invalid room UUID');
} }
return { return {
@ -94,10 +96,11 @@ export class RoomService {
}, },
}; };
} catch (err) { } catch (err) {
throw new HttpException( if (err instanceof BadRequestException) {
err.message, throw err; // Re-throw BadRequestException
err.status || HttpStatus.INTERNAL_SERVER_ERROR, } else {
); throw new HttpException('Room not found', HttpStatus.NOT_FOUND);
}
} }
} }
async renameRoomByUuid( async renameRoomByUuid(

View File

@ -35,7 +35,7 @@ export class UnitController {
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
@ -48,16 +48,12 @@ export class UnitController {
const unit = await this.unitService.getUnitByUuid(unitUuid); const unit = await this.unitService.getUnitByUuid(unitUuid);
return unit; return unit;
} catch (error) { } catch (error) {
if (error.status === 404) {
throw new HttpException('Unit not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
}
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -70,16 +66,12 @@ export class UnitController {
const unit = await this.unitService.getUnitChildByUuid(unitUuid, query); const unit = await this.unitService.getUnitChildByUuid(unitUuid, query);
return unit; return unit;
} catch (error) { } catch (error) {
if (error.status === 404) {
throw new HttpException('Unit not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
}
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get('parent/:unitUuid') @Get('parent/:unitUuid')
@ -88,16 +80,12 @@ export class UnitController {
const unit = await this.unitService.getUnitParentByUuid(unitUuid); const unit = await this.unitService.getUnitParentByUuid(unitUuid);
return unit; return unit;
} catch (error) { } catch (error) {
if (error.status === 404) {
throw new HttpException('Unit not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
}
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -113,9 +101,6 @@ export class UnitController {
); );
return unit; return unit;
} catch (error) { } catch (error) {
if (error.status === 404) {
throw new HttpException('Unit not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
@ -123,4 +108,3 @@ export class UnitController {
} }
} }
} }
}

View File

@ -53,8 +53,8 @@ export class UnitService {
}, },
relations: ['spaceType'], relations: ['spaceType'],
}); });
if (!unit) { if (!unit || !unit.spaceType || unit.spaceType.type !== 'unit') {
throw new HttpException('Unit not found', HttpStatus.NOT_FOUND); throw new BadRequestException('Invalid unit UUID');
} }
return { return {
uuid: unit.uuid, uuid: unit.uuid,
@ -64,16 +64,18 @@ export class UnitService {
type: unit.spaceType.type, type: unit.spaceType.type,
}; };
} catch (err) { } catch (err) {
throw new HttpException( if (err instanceof BadRequestException) {
err.message, throw err; // Re-throw BadRequestException
err.status || HttpStatus.INTERNAL_SERVER_ERROR, } else {
); throw new HttpException('Unit not found', HttpStatus.NOT_FOUND);
}
} }
} }
async getUnitChildByUuid( async getUnitChildByUuid(
unitUuid: string, unitUuid: string,
getUnitChildDto: GetUnitChildDto, getUnitChildDto: GetUnitChildDto,
): Promise<UnitChildInterface> { ): Promise<UnitChildInterface> {
try {
const { includeSubSpaces, page, pageSize } = getUnitChildDto; const { includeSubSpaces, page, pageSize } = getUnitChildDto;
const space = await this.spaceRepository.findOneOrFail({ const space = await this.spaceRepository.findOneOrFail({
@ -81,8 +83,8 @@ export class UnitService {
relations: ['children', 'spaceType'], relations: ['children', 'spaceType'],
}); });
if (space.spaceType.type !== 'unit') { if (!space || !space.spaceType || space.spaceType.type !== 'unit') {
throw new HttpException('Unit not found', HttpStatus.NOT_FOUND); throw new BadRequestException('Invalid unit UUID');
} }
const totalCount = await this.spaceRepository.count({ const totalCount = await this.spaceRepository.count({
@ -103,6 +105,13 @@ export class UnitService {
totalCount, totalCount,
children, children,
}; };
} catch (err) {
if (err instanceof BadRequestException) {
throw err; // Re-throw BadRequestException
} else {
throw new HttpException('Unit not found', HttpStatus.NOT_FOUND);
}
}
} }
private async buildHierarchy( private async buildHierarchy(
@ -165,10 +174,9 @@ export class UnitService {
}, },
relations: ['spaceType', 'parent', 'parent.spaceType'], relations: ['spaceType', 'parent', 'parent.spaceType'],
}); });
if (!unit) { if (!unit || !unit.spaceType || unit.spaceType.type !== 'unit') {
throw new HttpException('Unit not found', HttpStatus.NOT_FOUND); throw new BadRequestException('Invalid unit UUID');
} }
return { return {
uuid: unit.uuid, uuid: unit.uuid,
name: unit.spaceName, name: unit.spaceName,
@ -180,10 +188,11 @@ export class UnitService {
}, },
}; };
} catch (err) { } catch (err) {
throw new HttpException( if (err instanceof BadRequestException) {
err.message, throw err; // Re-throw BadRequestException
err.status || HttpStatus.INTERNAL_SERVER_ERROR, } else {
); throw new HttpException('Unit not found', HttpStatus.NOT_FOUND);
}
} }
} }