Skip to content

Commit

Permalink
refactor(logging): enhance error logging with stack information acros…
Browse files Browse the repository at this point in the history
…s multiple modules
  • Loading branch information
Anuj-Gupta4 committed Nov 15, 2024
1 parent dfcc62c commit 6188fa3
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/backend/app/auth/auth_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ async def get_or_create_user(

except Exception as e:
await db.rollback()
log.error(f"Exception occurred: {e}")
log.exception(f"Exception occurred: {e}", stack_info=True)
if 'duplicate key value violates unique constraint "users_username_key"' in str(
e
):
Expand Down
5 changes: 3 additions & 2 deletions src/backend/app/auth/osm.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ async def login_required(
try:
token_data = verify_token(access_token)
except ValueError as e:
log.error(e)
log.error("Failed to deserialise access token")
log.exception(
f"Failed to deserialise access token. Error: {e}", stack_info=True
)
raise HTTPException(
status_code=HTTPStatus.UNAUTHORIZED,
detail="Access token not valid",
Expand Down
7 changes: 5 additions & 2 deletions src/backend/app/auth/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ async def get_uid(user_data: AuthUser | DbUser) -> int:
return user_id

except Exception as e:
log.error(e)
log.error(f"Failed to get user id from auth object: {user_data}")
log.exception(
f"Failed to get user id from auth object: {user_data}. ",
f"Error: {e}",
stack_info=True,
)
raise HTTPException(
status_code=HTTPStatus.UNAUTHORIZED,
detail="Auth failed. No user id present",
Expand Down
18 changes: 9 additions & 9 deletions src/backend/app/central/central_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def get_odk_project(odk_central: Optional[central_schemas.ODKCentralDecrypted] =
),
) from e
except Exception as e:
log.exception(e)
log.exception(f"Error: {e}", stack_info=True)
raise HTTPException(
HTTPStatus.INTERNAL_SERVER_ERROR,
detail=f"Error creating project on ODK Central: {e}",
Expand All @@ -88,7 +88,7 @@ def get_odk_form(odk_central: central_schemas.ODKCentralDecrypted):
log.debug(f"Connecting to ODKCentral: url={url} user={user}")
form = OdkForm(url, user, pw)
except Exception as e:
log.error(e)
log.exception(f"Error: {e}", stack_info=True)
raise HTTPException(
HTTPStatus.INTERNAL_SERVER_ERROR,
detail=f"Error creating project on ODK Central: {e}",
Expand All @@ -114,7 +114,7 @@ def get_odk_app_user(odk_central: Optional[central_schemas.ODKCentralDecrypted]
log.debug(f"Connecting to ODKCentral: url={url} user={user}")
form = OdkAppUser(url, user, pw)
except Exception as e:
log.error(e)
log.exception(f"Error: {e}", stack_info=True)
raise HTTPException(
HTTPStatus.INTERNAL_SERVER_ERROR,
detail=f"Error creating project on ODK Central: {e}",
Expand Down Expand Up @@ -156,7 +156,7 @@ def create_odk_project(
log.info(f"Project {name} available on the ODK Central server.")
return result
except Exception as e:
log.error(e)
log.exception(f"Error: {e}", stack_info=True)
raise HTTPException(
HTTPStatus.INTERNAL_SERVER_ERROR,
detail=f"Error creating project on ODK Central: {e}",
Expand Down Expand Up @@ -206,7 +206,7 @@ def create_odk_xform(
try:
xform = get_odk_form(odk_credentials)
except Exception as e:
log.error(e)
log.exception(f"Error: {e}", stack_info=True)
raise HTTPException(
HTTPStatus.INTERNAL_SERVER_ERROR,
detail={"message": "Connection failed to odk central"},
Expand Down Expand Up @@ -278,7 +278,7 @@ async def get_form_list(db: Connection) -> list:
try:
return await DbXLSForm.all(db)
except Exception as e:
log.error(e)
log.exception(f"Error: {e}", stack_info=True)
raise HTTPException(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
detail=str(e),
Expand All @@ -302,7 +302,7 @@ async def read_and_test_xform(input_data: BytesIO) -> None:
# NOTE pyxform.xls2xform.convert returns a ConvertResult object
return BytesIO(xform_convert(input_data).xform.encode("utf-8"))
except Exception as e:
log.error(e)
log.exception(f"Error: {e}", stack_info=True)
msg = f"XLSForm is invalid: {str(e)}"
raise HTTPException(
status_code=HTTPStatus.UNPROCESSABLE_ENTITY, detail=msg
Expand Down Expand Up @@ -688,8 +688,8 @@ async def get_entities_data(
url_params=f"$select=__id{',' if fields else ''} {fields}",
)
except Exception as e:
log.exception(f"Error: {e}", stack_info=True)
msg = f"Getting entity data failed for ODK project ({odk_id})"
log.error(msg)
raise HTTPException(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
detail=msg,
Expand Down Expand Up @@ -928,7 +928,7 @@ async def get_appuser_token(
return f"{odk_url}/v1/key/{appuser_token}/projects/{project_odk_id}"

except Exception as e:
log.error(f"An error occurred: {str(e)}")
log.exception(f"An error occurred: {str(e)}", stack_info=True)
raise HTTPException(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
detail="An error occurred while creating the app user token.",
Expand Down
2 changes: 1 addition & 1 deletion src/backend/app/central/central_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ async def refresh_appuser_token(
)

except Exception as e:
log.error(e)
log.exception(f"Error: {e}", stack_info=True)
msg = f"failed to refresh the appuser token for project {project_id}"
log.error(msg)
raise HTTPException(
Expand Down
8 changes: 5 additions & 3 deletions src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,10 @@ async def read_and_insert_xlsforms(db: Connection, directory: str) -> None:
log.info(f"XLSForm for '{category}' inserted/updated in the database")

except Exception as e:
log.error(
f"Failed to insert or update {category} in the database. Error: {e}"
log.exception(
f"Failed to insert or update {category} in the database. "
f"Error: {e}",
stack_info=True,
)

# Determine the forms that need to be deleted (those in the DB but
Expand Down Expand Up @@ -777,7 +779,7 @@ def generate_project_basemap(

except Exception as e:
log.debug(str(format_exc()))
log.exception(str(e))
log.exception(f"Error: {e}", stack_info=True)
log.error(f"Tiles generation process failed for project id {project_id}")

if new_basemap:
Expand Down
14 changes: 9 additions & 5 deletions src/backend/app/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,11 @@ def copy_obj_bucket_to_bucket(
)

except Exception as e:
log.exception(e)
log.error(f"Failed to copy object {source_path} to new bucket: {dest_bucket}")
log.exception(
f"Failed to copy object {source_path} to new bucket {dest_bucket}. "
f"Error: {e}",
stack_info=True,
)
return False

return True
Expand Down Expand Up @@ -237,9 +240,10 @@ async def delete_all_objs_under_prefix(bucket_name: str, s3_path: str) -> bool:
return False

except Exception as e:
log.exception(e)
log.error(
f"Failed to delete bucket ({bucket_name}) files under path: {s3_path}"
log.exception(
f"Failed to delete bucket ({bucket_name}) files under path: {s3_path}. "
f"Error: {e}",
stack_info=True,
)
return False

Expand Down

0 comments on commit 6188fa3

Please sign in to comment.