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,
);
}
}
}