Skip to content

Commit

Permalink
feat: logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dartt0n committed Jul 20, 2024
1 parent 2c36b3b commit ae9ee0c
Show file tree
Hide file tree
Showing 13 changed files with 697 additions and 60 deletions.
1 change: 0 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ test path="./src/tests" n_workers="8":
trap 'echo; docker stop randorm-api-tests-mongo' EXIT # remove container on exit
docker run -d -p 27017:27017 --rm --name randorm-api-tests-mongo mongodb/mongodb-community-server:latest
poetry run pytest {{ path }} -n {{ n_workers }} --cov=./ --cov-report=html --cov-report=term-missing


run-server n_workers="1":
poetry run gunicorn main:app --bind localhost:8080 --workers {{ n_workers }} --worker-class aiohttp.GunicornWebWorker
Expand Down
6 changes: 6 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from src.adapter.internal.database.memorydb.service import MemoryDBAdapter
from src.app.http.server import build_server
from src.service.allocation import AllocationService
from src.service.answer import AnswerService
from src.service.form_field import FormFieldService
from src.service.participant import ParticipantService
from src.service.preference import PreferenceService
Expand Down Expand Up @@ -54,11 +55,16 @@ async def app():
room_repo=repo,
user_repo=repo,
)
answer_service = AnswerService(
form_field_repo=repo,
participant_repo=repo,
)

oauth_adapter = TelegramOauthAdapter(secret_token, jwt_secret, user_service)

return build_server(
user_service,
answer_service,
allocation_service,
form_field_service,
participant_service,
Expand Down
32 changes: 17 additions & 15 deletions src/adapter/external/auth/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,20 @@ async def register(self, data: Any) -> TgOauthContainer:
CreateUser(telegram_id=request.id, profile=request.profile)
)
except (ValidationError, database_exception.ReflectUserException) as e:
# log.error(
# f"failed to build callback data or reflect user data to create user with exception: ", e
# )
log.error(
"failed to build callback data or reflect user data to create user with exception: {}",
e,
)
raise auth_exception.InvalidCredentialsException(
"failed to reflect user data to create user"
) from e
except service_exception.CreateUserException as e:
log.error(f"creating new user failed with service exception: {e}")
log.error("creating new user failed with service exception: {}", e)
raise auth_exception.UserAlreadyExistsException(
"creating new user failed"
) from e
except auth_exception.AuthException as e:
log.error(f"authentiocation failed with auth exception: {e}")
log.error("authentiocation failed with auth exception: {}", e)
raise e

return TgOauthContainer.construct(
Expand All @@ -150,17 +151,18 @@ async def login(self, data: Any) -> TgOauthContainer:

user = await self.__service.find_by_telegram_id(request.id)
except (ValidationError, database_exception.ReflectUserException) as e:
# log.error(
# f"failed to build callback data or reflect user data to read user with exception: {e}"
# )
log.error(
"failed to build callback data or reflect user data to read user with exception: {}",
e,
)
raise auth_exception.InvalidCredentialsException(
"failed to reflect user data to read user"
) from e
except service_exception.ReadUserException as e:
log.error(f"reading user failed with service exception: {e}")
log.error("reading user failed with service exception: {}", e)
raise auth_exception.UserNotFoundException("reading user failed") from e
except auth_exception.AuthException as e:
log.error(f"authentiocation failed with auth exception: {e}")
log.error("authentiocation failed with auth exception: {}", e)
raise e

return TgOauthContainer.construct(
Expand All @@ -180,19 +182,19 @@ async def retrieve_user(self, data: TgOauthContainer) -> User:
log.debug("building dto from container")
dto = data.to_dto(self.__jwt_secret)
except Exception as e:
log.error(f"failed to build dto from container with exception: {e}")
log.error("failed to build dto from container with exception: {}", e)
raise auth_exception.InvalidCredentialsException(
"invalid data type"
) from e

user = await self.__service.read(ReadUser(_id=dto.id))
except (ValidationError, AttributeError) as e:
# log.error(
# "failed to construct dto or reflect to read user with exception: {e}", e
# )
log.error(
"failed to construct dto or reflect to read user with exception: {}", e
)
raise auth_exception.InvalidCredentialsException("invalid data type") from e
except service_exception.ReadUserException as e:
log.error(f"reading user failed with service exception: {e}")
log.error("reading user failed with service exception: {}", e)
raise auth_exception.UserNotFoundException("user not found") from e
else:
log.debug(
Expand Down
Loading

0 comments on commit ae9ee0c

Please sign in to comment.