Skip to content

Commit

Permalink
#2: Change how exceptions are logged
Browse files Browse the repository at this point in the history
  • Loading branch information
keiranjprice101 committed Jun 21, 2019
1 parent 36352dc commit c45075a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
14 changes: 7 additions & 7 deletions common/database_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def get_row_by_id(table, id):
session.close()
return result
session.close()
raise MissingRecordError()
raise MissingRecordError(f" Could not find record in {table.__tablename__} with ID: {id}")


def delete_row_by_id(table, id):
Expand All @@ -90,7 +90,7 @@ def delete_row_by_id(table, id):
session.close()
return
session.close()
raise MissingRecordError()
raise MissingRecordError(f" Could not find record in {table.__tablename__} with ID: {id}")


def update_row_from_id(table, id, new_values):
Expand All @@ -110,7 +110,7 @@ def update_row_from_id(table, id, new_values):
session.close()
return
session.close()
raise MissingRecordError()
raise MissingRecordError(f" Could not find record in {table.__tablename__} with ID: {id}")


def get_rows_by_filter(table, filters):
Expand Down Expand Up @@ -140,14 +140,14 @@ def get_rows_by_filter(table, filters):
elif direction.upper() == "DESC":
base_query = base_query.from_self().order_by(desc(getattr(table, field)))
else:
raise BadFilterError()
raise BadFilterError(f" Bad filter given, filter: {filter}")
else:
if direction.upper() == "ASC":
base_query = base_query.order_by(asc(getattr(table, field)))
elif direction.upper() == "DESC":
base_query = base_query.order_by(desc(getattr(table, field)))
else:
raise BadFilterError()
raise BadFilterError(f" Bad filter given, filter: {filter}")
elif list(filter)[0].lower() == "skip":
for key in filter:
skip = filter[key]
Expand All @@ -160,7 +160,7 @@ def get_rows_by_filter(table, filters):
limit = filter[key]
base_query = base_query.limit(limit)
else:
raise BadFilterError()
raise BadFilterError(f"Invalid filters provided recieved {filters}")
log.info(" Closing DB session")
session.close()
return list(map(lambda x: x.to_dict(), base_query.all()))
Expand Down Expand Up @@ -211,6 +211,6 @@ def patch_entities(table, json_list):
result = get_row_by_id(table, entity[key])
results.append(result)
if len(results) == 0:
raise BadRequestError()
raise BadRequestError(f" Bad request made, request: {json_list}")

return results
16 changes: 5 additions & 11 deletions common/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,20 @@


class ApiError(Exception):
def __init__(self):
log.info(" ApiError(): An error has been raised.")
pass


class MissingRecordError(ApiError):
def __init__(self):
log.info(" MissingRecordError(): Record not found, DB session Closed")

pass


class BadFilterError(ApiError):
def __init__(self):
log.info(" BadFilterError(): Bad filter supplied")
pass


class AuthenticationError(ApiError):
def __init__(self):
log.info(" AuthenticationError(): Error authenticating consumer")
pass


class BadRequestError(ApiError):
def __init__(self):
log.info(" BadRequestError(): Bad request by Consumer")
pass
2 changes: 1 addition & 1 deletion common/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def get_session_id_from_auth_header():
if auth_header == "":
return ""
if len(auth_header) != 2 or auth_header[0] != "Bearer":
raise AuthenticationError()
raise AuthenticationError(f" Could not authenticate consumer with auth header {auth_header}")
return auth_header[1]


Expand Down

0 comments on commit c45075a

Please sign in to comment.