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
+19 -35
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,
);
}
}
}
+48 -38
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);
}
}
}