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