Refactor error handling in DeviceService

This commit is contained in:
faris Aljohari
2024-04-28 11:53:06 +03:00
parent cd2f496670
commit 2a6cce345d

View File

@ -125,9 +125,15 @@ export class DeviceService {
return { message: 'device added in room successfully' };
} catch (error) {
if (error.code === '23505') {
throw new Error('Device already exists in the room.');
throw new HttpException(
'Device already exists in the room',
HttpStatus.BAD_REQUEST,
);
} else {
throw new Error('Failed to add device in room');
throw new HttpException(
'Failed to add device in room',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}
@ -141,9 +147,15 @@ export class DeviceService {
return { message: 'device added in group successfully' };
} catch (error) {
if (error.code === '23505') {
throw new Error('Device already exists in the group.');
throw new HttpException(
'Device already exists in the group',
HttpStatus.BAD_REQUEST,
);
} else {
throw new Error('Failed to add device in group');
throw new HttpException(
'Failed to add device in group',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}