Skip to content

Commit

Permalink
Merge pull request #5927 from hotosm/enhance/5912-error-handling
Browse files Browse the repository at this point in the history
Apply newly defined `NotFound` exceptions to handle resource not found errors.
  • Loading branch information
Aadesh-Baral authored Aug 9, 2023
2 parents fff19e8 + 4e009cd commit 43556b0
Show file tree
Hide file tree
Showing 80 changed files with 829 additions and 981 deletions.
42 changes: 11 additions & 31 deletions backend/api/annotations/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from schematics.exceptions import DataError
from backend.models.postgis.task import Task
from backend.models.postgis.task_annotation import TaskAnnotation
from backend.services.project_service import ProjectService, NotFound
from backend.services.project_service import ProjectService
from backend.services.task_annotations_service import TaskAnnotationsService
from backend.services.application_service import ApplicationService

Expand Down Expand Up @@ -35,24 +35,14 @@ def get(self, project_id: int, annotation_type: str = None):
500:
description: Internal Server Error
"""
try:
ProjectService.exists(project_id)
except NotFound as e:
current_app.logger.error(f"Error validating project: {str(e)}")
return {"Error": "Project not found", "SubCode": "NotFound"}, 404

try:
if annotation_type:
annotations = TaskAnnotation.get_task_annotations_by_project_id_type(
project_id, annotation_type
)
else:
annotations = TaskAnnotation.get_task_annotations_by_project_id(
project_id
)
return annotations.to_primitive(), 200
except NotFound:
return {"Error": "Annotations not found", "SubCode": "NotFound"}, 404
ProjectService.exists(project_id)
if annotation_type:
annotations = TaskAnnotation.get_task_annotations_by_project_id_type(
project_id, annotation_type
)
else:
annotations = TaskAnnotation.get_task_annotations_by_project_id(project_id)
return annotations.to_primitive(), 200

def post(self, project_id: int, annotation_type: str):
"""
Expand Down Expand Up @@ -122,13 +112,7 @@ def post(self, project_id: int, annotation_type: str):

if "Application-Token" in request.headers:
application_token = request.headers["Application-Token"]
try:
is_valid_token = ApplicationService.check_token( # noqa
application_token
)
except NotFound:
current_app.logger.error("Invalid token")
return {"Error": "Invalid token", "SubCode": "NotFound"}, 500
is_valid_token = ApplicationService.check_token(application_token) # noqa
else:
current_app.logger.error("No token supplied")
return {"Error": "No token supplied", "SubCode": "NotFound"}, 500
Expand All @@ -138,11 +122,7 @@ def post(self, project_id: int, annotation_type: str):
except DataError as e:
current_app.logger.error(f"Error validating request: {str(e)}")

try:
ProjectService.exists(project_id)
except NotFound as e:
current_app.logger.error(f"Error validating project: {str(e)}")
return {"Error": "Project not found", "SubCode": "NotFound"}, 404
ProjectService.exists(project_id)

task_ids = [t["taskId"] for t in annotations["tasks"]]

Expand Down
31 changes: 11 additions & 20 deletions backend/api/campaigns/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from backend.models.dtos.campaign_dto import CampaignDTO, NewCampaignDTO
from backend.services.campaign_service import CampaignService
from backend.services.organisation_service import OrganisationService
from backend.models.postgis.utils import NotFound
from backend.services.users.authentication_service import token_auth


Expand Down Expand Up @@ -43,17 +42,14 @@ def get(self, campaign_id):
500:
description: Internal Server Error
"""
try:
authenticated_user_id = token_auth.current_user()
if authenticated_user_id:
campaign = CampaignService.get_campaign_as_dto(
campaign_id, authenticated_user_id
)
else:
campaign = CampaignService.get_campaign_as_dto(campaign_id, 0)
return campaign.to_primitive(), 200
except NotFound:
return {"Error": "No campaign found", "SubCode": "NotFound"}, 404
authenticated_user_id = token_auth.current_user()
if authenticated_user_id:
campaign = CampaignService.get_campaign_as_dto(
campaign_id, authenticated_user_id
)
else:
campaign = CampaignService.get_campaign_as_dto(campaign_id, 0)
return campaign.to_primitive(), 200

@token_auth.login_required
def patch(self, campaign_id):
Expand Down Expand Up @@ -139,8 +135,6 @@ def patch(self, campaign_id):
try:
campaign = CampaignService.update_campaign(campaign_dto, campaign_id)
return {"Success": "Campaign {} updated".format(campaign.id)}, 200
except NotFound:
return {"Error": "Campaign not found", "SubCode": "NotFound"}, 404
except ValueError:
error_msg = "Campaign PATCH - name already exists"
return {"Error": error_msg, "SubCode": "NameExists"}, 409
Expand Down Expand Up @@ -195,12 +189,9 @@ def delete(self, campaign_id):
error_msg = f"CampaignsRestAPI DELETE: {str(e)}"
return {"Error": error_msg, "SubCode": "UserNotPermitted"}, 403

try:
campaign = CampaignService.get_campaign(campaign_id)
CampaignService.delete_campaign(campaign.id)
return {"Success": "Campaign deleted"}, 200
except NotFound:
return {"Error": "Campaign not found", "SubCode": "NotFound"}, 404
campaign = CampaignService.get_campaign(campaign_id)
CampaignService.delete_campaign(campaign.id)
return {"Success": "Campaign deleted"}, 200


class CampaignsAllAPI(Resource):
Expand Down
25 changes: 5 additions & 20 deletions backend/api/comments/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from backend.models.dtos.message_dto import ChatMessageDTO
from backend.models.dtos.mapping_dto import TaskCommentDTO
from backend.models.postgis.utils import NotFound
from backend.services.messaging.chat_service import ChatService
from backend.services.users.user_service import UserService
from backend.services.project_service import ProjectService
Expand Down Expand Up @@ -109,19 +108,11 @@ def get(self, project_id):
500:
description: Internal Server Error
"""
try:
ProjectService.exists(project_id)
except NotFound as e:
current_app.logger.error(f"Error validating project: {str(e)}")
return {"Error": "Project not found", "SubCode": "NotFound"}, 404

try:
page = int(request.args.get("page")) if request.args.get("page") else 1
per_page = int(request.args.get("perPage", 20))
project_messages = ChatService.get_messages(project_id, page, per_page)
return project_messages.to_primitive(), 200
except NotFound:
return {"Error": "Project not found", "SubCode": "NotFound"}, 404
ProjectService.exists(project_id)
page = int(request.args.get("page")) if request.args.get("page") else 1
per_page = int(request.args.get("perPage", 20))
project_messages = ChatService.get_messages(project_id, page, per_page)
return project_messages.to_primitive(), 200


class CommentsProjectsRestAPI(Resource):
Expand Down Expand Up @@ -169,8 +160,6 @@ def delete(self, project_id, comment_id):
project_id, comment_id, authenticated_user_id
)
return {"Success": "Comment deleted"}, 200
except NotFound:
return {"Error": "Comment not found", "SubCode": "NotFound"}, 404
except ValueError as e:
return {"Error": str(e).split("-")[1], "SubCode": str(e).split("-")[0]}, 403

Expand Down Expand Up @@ -248,8 +237,6 @@ def post(self, project_id, task_id):
try:
task = MappingService.add_task_comment(task_comment)
return task.to_primitive(), 201
except NotFound:
return {"Error": "Task Not Found", "SubCode": "NotFound"}, 404
except MappingServiceError:
return {"Error": "Task update failed"}, 403

Expand Down Expand Up @@ -320,7 +307,5 @@ def get(self, project_id, task_id):
# task = MappingService.add_task_comment(task_comment)
# return task.to_primitive(), 200
return
except NotFound:
return {"Error": "Task Not Found", "SubCode": "NotFound"}, 404
except MappingServiceError as e:
return {"Error": str(e)}, 403
22 changes: 6 additions & 16 deletions backend/api/interests/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from schematics.exceptions import DataError

from backend.models.dtos.interests_dto import InterestDTO
from backend.models.postgis.utils import NotFound
from backend.services.interests_service import InterestService
from backend.services.organisation_service import OrganisationService
from backend.services.users.authentication_service import token_auth
Expand Down Expand Up @@ -144,11 +143,8 @@ def get(self, interest_id):
error_msg = f"InterestsRestAPI GET: {str(e)}"
return {"Error": error_msg, "SubCode": "UserNotPermitted"}, 403

try:
interest = InterestService.get(interest_id)
return interest.to_primitive(), 200
except NotFound:
return {"Error": INTEREST_NOT_FOUND, "SubCode": "NotFound"}, 404
interest = InterestService.get(interest_id)
return interest.to_primitive(), 200

@token_auth.login_required
def patch(self, interest_id):
Expand Down Expand Up @@ -212,11 +208,8 @@ def patch(self, interest_id):
current_app.logger.error(f"Error validating request: {str(e)}")
return {"Error": str(e), "SubCode": "InvalidData"}, 400

try:
update_interest = InterestService.update(interest_id, interest_dto)
return update_interest.to_primitive(), 200
except NotFound:
return {"Error": INTEREST_NOT_FOUND, "SubCode": "NotFound"}, 404
update_interest = InterestService.update(interest_id, interest_dto)
return update_interest.to_primitive(), 200

@token_auth.login_required
def delete(self, interest_id):
Expand Down Expand Up @@ -262,8 +255,5 @@ def delete(self, interest_id):
error_msg = f"InterestsRestAPI DELETE: {str(e)}"
return {"Error": error_msg, "SubCode": "UserNotPermitted"}, 403

try:
InterestService.delete(interest_id)
return {"Success": "Interest deleted"}, 200
except NotFound:
return {"Error": INTEREST_NOT_FOUND, "SubCode": "NotFound"}, 404
InterestService.delete(interest_id)
return {"Success": "Interest deleted"}, 200
41 changes: 10 additions & 31 deletions backend/api/issues/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from schematics.exceptions import DataError

from backend.models.dtos.mapping_issues_dto import MappingIssueCategoryDTO
from backend.models.postgis.utils import NotFound
from backend.services.mapping_issues_service import MappingIssueCategoryService
from backend.services.users.authentication_service import token_auth, tm

Expand Down Expand Up @@ -33,18 +32,10 @@ def get(self, category_id):
500:
description: Internal Server Error
"""
try:
category_dto = (
MappingIssueCategoryService.get_mapping_issue_category_as_dto(
category_id
)
)
return category_dto.to_primitive(), 200
except NotFound:
return {
"Error": ISSUE_NOT_FOUND,
"SubCode": "NotFound",
}, 404
category_dto = MappingIssueCategoryService.get_mapping_issue_category_as_dto(
category_id
)
return category_dto.to_primitive(), 200

@tm.pm_only()
@token_auth.login_required
Expand Down Expand Up @@ -102,16 +93,10 @@ def patch(self, category_id):
"SubCode": "InvalidData",
}, 400

try:
updated_category = (
MappingIssueCategoryService.update_mapping_issue_category(category_dto)
)
return updated_category.to_primitive(), 200
except NotFound:
return {
"Error": ISSUE_NOT_FOUND,
"SubCode": "NotFound",
}, 404
updated_category = MappingIssueCategoryService.update_mapping_issue_category(
category_dto
)
return updated_category.to_primitive(), 200

@tm.pm_only()
@token_auth.login_required
Expand Down Expand Up @@ -149,14 +134,8 @@ def delete(self, category_id):
500:
description: Internal Server Error
"""
try:
MappingIssueCategoryService.delete_mapping_issue_category(category_id)
return {"Success": "Mapping-issue category deleted"}, 200
except NotFound:
return {
"Error": ISSUE_NOT_FOUND,
"SubCode": "NotFound",
}, 404
MappingIssueCategoryService.delete_mapping_issue_category(category_id)
return {"Success": "Mapping-issue category deleted"}, 200


class IssuesAllAPI(Resource):
Expand Down
9 changes: 3 additions & 6 deletions backend/api/licenses/actions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from flask_restful import Resource

from backend.services.users.authentication_service import token_auth
from backend.services.users.user_service import UserService, NotFound
from backend.services.users.user_service import UserService


class LicensesActionsAcceptAPI(Resource):
Expand Down Expand Up @@ -37,8 +37,5 @@ def post(self, license_id):
500:
description: Internal Server Error
"""
try:
UserService.accept_license_terms(token_auth.current_user(), license_id)
return {"Success": "Terms Accepted"}, 200
except NotFound:
return {"Error": "User or License not found", "SubCode": "NotFound"}, 404
UserService.accept_license_terms(token_auth.current_user(), license_id)
return {"Success": "Terms Accepted"}, 200
29 changes: 8 additions & 21 deletions backend/api/licenses/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from schematics.exceptions import DataError

from backend.models.dtos.licenses_dto import LicenseDTO
from backend.models.postgis.utils import NotFound
from backend.services.license_service import LicenseService
from backend.services.users.authentication_service import token_auth, tm

Expand Down Expand Up @@ -86,11 +85,8 @@ def get(self, license_id):
500:
description: Internal Server Error
"""
try:
license_dto = LicenseService.get_license_as_dto(license_id)
return license_dto.to_primitive(), 200
except NotFound:
return {"Error": "License Not Found", "SubCode": "NotFound"}, 404
license_dto = LicenseService.get_license_as_dto(license_id)
return license_dto.to_primitive(), 200

@tm.pm_only()
@token_auth.login_required
Expand Down Expand Up @@ -148,11 +144,8 @@ def patch(self, license_id):
current_app.logger.error(f"Error validating request: {str(e)}")
return {"Error": str(e), "SubCode": "InvalidData"}, 400

try:
updated_license = LicenseService.update_licence(license_dto)
return updated_license.to_primitive(), 200
except NotFound:
return {"Error": "License Not Found", "SubCode": "NotFound"}, 404
updated_license = LicenseService.update_licence(license_dto)
return updated_license.to_primitive(), 200

@tm.pm_only()
@token_auth.login_required
Expand Down Expand Up @@ -187,11 +180,8 @@ def delete(self, license_id):
500:
description: Internal Server Error
"""
try:
LicenseService.delete_license(license_id)
return {"Success": "License deleted"}, 200
except NotFound:
return {"Error": "License Not Found", "SubCode": "NotFound"}, 404
LicenseService.delete_license(license_id)
return {"Success": "License deleted"}, 200


class LicensesAllAPI(Resource):
Expand All @@ -211,8 +201,5 @@ def get(self):
500:
description: Internal Server Error
"""
try:
licenses_dto = LicenseService.get_all_licenses()
return licenses_dto.to_primitive(), 200
except NotFound:
return {"Error": "License Not Found", "SubCode": "NotFound"}, 404
licenses_dto = LicenseService.get_all_licenses()
return licenses_dto.to_primitive(), 200
Loading

0 comments on commit 43556b0

Please sign in to comment.