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) {
throw new HttpException(
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ -49,14 +49,10 @@ export class BuildingController {
await this.buildingService.getBuildingByUuid(buildingUuid);
return building;
} catch (error) {
if (error.status === 404) {
throw new HttpException('Building not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException(
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ -74,14 +70,10 @@ export class BuildingController {
);
return building;
} catch (error) {
if (error.status === 404) {
throw new HttpException('Building not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException(
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@ -93,14 +85,10 @@ export class BuildingController {
await this.buildingService.getBuildingParentByUuid(buildingUuid);
return building;
} catch (error) {
if (error.status === 404) {
throw new HttpException('Building not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException(
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@ -117,14 +105,10 @@ export class BuildingController {
);
return building;
} catch (error) {
if (error.status === 404) {
throw new HttpException('Building not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}

View File

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

View File

@ -35,7 +35,7 @@ export class CommunityController {
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ -49,14 +49,10 @@ export class CommunityController {
await this.communityService.getCommunityByUuid(communityUuid);
return community;
} catch (error) {
if (error.status === 404) {
throw new HttpException('Community not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException(
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ -74,14 +70,10 @@ export class CommunityController {
);
return community;
} catch (error) {
if (error.status === 404) {
throw new HttpException('Community not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException(
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ -99,14 +91,10 @@ export class CommunityController {
);
return community;
} catch (error) {
if (error.status === 404) {
throw new HttpException('Community not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}

View File

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

View File

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

View File

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

View File

@ -33,7 +33,7 @@ export class RoomController {
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ -46,14 +46,10 @@ export class RoomController {
const room = await this.roomService.getRoomByUuid(roomUuid);
return room;
} catch (error) {
if (error.status === 404) {
throw new HttpException('Room not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException(
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ -65,14 +61,10 @@ export class RoomController {
const room = await this.roomService.getRoomParentByUuid(roomUuid);
return room;
} catch (error) {
if (error.status === 404) {
throw new HttpException('Room not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException(
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ -90,14 +82,10 @@ export class RoomController {
);
return room;
} catch (error) {
if (error.status === 404) {
throw new HttpException('Room not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}

View File

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

View File

@ -35,7 +35,7 @@ export class UnitController {
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ -48,14 +48,10 @@ export class UnitController {
const unit = await this.unitService.getUnitByUuid(unitUuid);
return unit;
} catch (error) {
if (error.status === 404) {
throw new HttpException('Unit not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException(
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ -70,14 +66,10 @@ export class UnitController {
const unit = await this.unitService.getUnitChildByUuid(unitUuid, query);
return unit;
} catch (error) {
if (error.status === 404) {
throw new HttpException('Unit not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException(
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@ -88,14 +80,10 @@ export class UnitController {
const unit = await this.unitService.getUnitParentByUuid(unitUuid);
return unit;
} catch (error) {
if (error.status === 404) {
throw new HttpException('Unit not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException(
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ -113,14 +101,10 @@ export class UnitController {
);
return unit;
} catch (error) {
if (error.status === 404) {
throw new HttpException('Unit not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}

View File

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