Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
- Enhances error handling and logging
- Fine tune hybrid locks on main repository initialization and finalization
  • Loading branch information
GabrielBarberini committed Jul 11, 2024
1 parent 559bdd0 commit b43cf4a
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 32 deletions.
20 changes: 20 additions & 0 deletions lib/controllers/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,15 @@ async def create_env(self) -> Union[EnvCreated, HTTPException]:
env_repo.fetch_env(self.env)
await env_repo.create_env()
except PyMongoError as e:
logger.error(
f"controllers.environment.create_env: PyMongoError {e}"
)
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Failed to create environment in db",
) from e
except HTTPException as e:
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"controllers.environment.create_env: {exc_str}")
Expand Down Expand Up @@ -89,10 +94,15 @@ async def get_env_by_id(env_id: str) -> Union[Env, HTTPException]:
await env_repo.get_env_by_id(env_id)
read_env = env_repo.env
except PyMongoError as e:
logger.error(
f"controllers.environment.get_env_by_id: PyMongoError {e}"
)
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Failed to read environment from db",
) from e
except HTTPException as e:
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"controllers.environment.get_env_by_id: {exc_str}")
Expand Down Expand Up @@ -173,10 +183,15 @@ async def update_env_by_id(
await env_repo.create_env()
await env_repo.delete_env_by_id(env_id)
except PyMongoError as e:
logger.error(
f"controllers.environment.update_env: PyMongoError {e}"
)
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Failed to update environment from db",
) from e
except HTTPException as e:
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"controllers.environment.update_env: {exc_str}")
Expand Down Expand Up @@ -211,10 +226,15 @@ async def delete_env_by_id(
async with EnvRepository() as env_repo:
await env_repo.delete_env_by_id(env_id)
except PyMongoError as e:
logger.error(
f"controllers.environment.delete_env: PyMongoError {e}"
)
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Failed to delete environment from db",
) from e
except HTTPException as e:
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"controllers.environment.delete_env: {exc_str}")
Expand Down
24 changes: 24 additions & 0 deletions lib/controllers/flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,13 @@ async def create_flight(self) -> Union[FlightCreated, HTTPException]:
rocket_option=self.rocket_option,
)
except PyMongoError as e:
logger.error(f"controllers.flight.create_flight: PyMongoError {e}")
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Failed to create flight in db",
) from e
except HTTPException as e:
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"controllers.flight.create_flight: {exc_str}")
Expand Down Expand Up @@ -150,10 +153,15 @@ async def get_flight_by_id(flight_id: str) -> Union[Flight, HTTPException]:
await flight_repo.get_flight_by_id(flight_id)
read_flight = flight_repo.flight
except PyMongoError as e:
logger.error(
f"controllers.flight.get_flight_by_id: PyMongoError {e}"
)
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Failed to read flight from db",
) from e
except HTTPException as e:
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"controllers.flight.get_flight_by_id: {exc_str}")
Expand Down Expand Up @@ -237,10 +245,13 @@ async def update_flight_by_id(
)
await flight_repo.delete_flight_by_id(flight_id)
except PyMongoError as e:
logger.error(f"controllers.flight.update_flight: PyMongoError {e}")
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Failed to update flight in db",
) from e
except HTTPException as e:
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"controllers.flight.update_flight: {exc_str}")
Expand Down Expand Up @@ -285,10 +296,15 @@ async def update_env_by_flight_id(
)
await flight_repo.delete_flight_by_id(flight_id)
except PyMongoError as e:
logger.error(
f"controllers.flight.update_env_by_flight_id: PyMongoError {e}"
)
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Failed to update environment from db",
) from e
except HTTPException as e:
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"controllers.flight.update_env: {exc_str}")
Expand Down Expand Up @@ -335,10 +351,15 @@ async def update_rocket_by_flight_id(
)
await flight_repo.delete_flight_by_id(flight_id)
except PyMongoError as e:
logger.error(
f"controllers.flight.update_rocket_by_flight_id: PyMongoError {e}"
)
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Failed to update rocket from db",
) from e
except HTTPException as e:
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"controllers.flight.update_rocket: {exc_str}")
Expand Down Expand Up @@ -373,10 +394,13 @@ async def delete_flight_by_id(
async with FlightRepository() as flight_repo:
await flight_repo.delete_flight_by_id(flight_id)
except PyMongoError as e:
logger.error(f"controllers.flight.delete_flight: PyMongoError {e}")
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Failed to delete flight from db",
) from e
except HTTPException as e:
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"controllers.flight.delete_flight: {exc_str}")
Expand Down
14 changes: 14 additions & 0 deletions lib/controllers/motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,13 @@ async def create_motor(self) -> Union[MotorCreated, HTTPException]:
motor_repo.fetch_motor(self.motor)
await motor_repo.create_motor(motor_kind=self.motor_kind)
except PyMongoError as e:
logger.error(f"controllers.motor.create_motor: PyMongoError {e}")
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Failed to create motor in db",
) from e
except HTTPException as e:
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"controllers.motor.create_motor: {exc_str}")
Expand Down Expand Up @@ -158,10 +161,15 @@ async def get_motor_by_id(motor_id: str) -> Union[Motor, HTTPException]:
await motor_repo.get_motor_by_id(motor_id)
read_motor = motor_repo.motor
except PyMongoError as e:
logger.error(
f"controllers.motor.get_motor_by_id: PyMongoError {e}"
)
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Failed to read motor from db",
) from e
except HTTPException as e:
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"controllers.motor.get_motor_by_id: {exc_str}")
Expand Down Expand Up @@ -242,10 +250,13 @@ async def update_motor_by_id(
await motor_repo.create_motor(motor_kind=self.motor_kind)
await motor_repo.delete_motor_by_id(motor_id)
except PyMongoError as e:
logger.error(f"controllers.motor.update_motor: PyMongoError {e}")
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Failed to update motor in db",
) from e
except HTTPException as e:
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"controllers.motor.update_motor: {exc_str}")
Expand Down Expand Up @@ -280,10 +291,13 @@ async def delete_motor_by_id(
async with MotorRepository() as motor_repo:
await motor_repo.delete_motor_by_id(motor_id)
except PyMongoError as e:
logger.error(f"controllers.motor.delete_motor: PyMongoError {e}")
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Failed to delete motor from db",
) from e
except HTTPException as e:
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"controllers.motor.delete_motor: {exc_str}")
Expand Down
14 changes: 14 additions & 0 deletions lib/controllers/rocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,13 @@ async def create_rocket(self) -> Union[RocketCreated, HTTPException]:
motor_kind=self.motor_kind,
)
except PyMongoError as e:
logger.error(f"controllers.rocket.create_rocket: PyMongoError {e}")
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail=f"Failed to create rocket in the db",
) from e
except HTTPException as e:
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"controllers.rocket.create_rocket: {exc_str}")
Expand Down Expand Up @@ -200,10 +203,15 @@ async def get_rocket_by_id(
await rocket_repo.get_rocket_by_id(rocket_id)
read_rocket = rocket_repo.rocket
except PyMongoError as e:
logger.error(
f"controllers.rocket.get_rocket_by_id: PyMongoError {e}"
)
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Failed to read rocket from db",
) from e
except HTTPException as e:
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"controllers.rocket.get_rocket_by_id: {exc_str}")
Expand Down Expand Up @@ -286,10 +294,13 @@ async def update_rocket_by_id(
)
await rocket_repo.delete_rocket_by_id(rocket_id)
except PyMongoError as e:
logger.error(f"controllers.rocket.update_rocket: PyMongoError {e}")
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Failed to update rocket in the db",
) from e
except HTTPException as e:
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"controllers.rocket.update_rocket: {exc_str}")
Expand Down Expand Up @@ -324,10 +335,13 @@ async def delete_rocket_by_id(
async with RocketRepository() as rocket_repo:
await rocket_repo.delete_rocket_by_id(rocket_id)
except PyMongoError as e:
logger.error(f"controllers.rocket.delete_rocket: PyMongoError {e}")
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
detail="Failed to delete rocket from db",
) from e
except HTTPException as e:
raise e from e
except Exception as e:
exc_str = parse_error(e)
logger.error(f"controllers.rocket.delete_rocket: {exc_str}")
Expand Down
Loading

0 comments on commit b43cf4a

Please sign in to comment.