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
+17 -33
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,
);
}
}
}
+55 -39
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);
}
}
}