Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(refactor): Move all custom API errors to errors.py #7019

Merged
merged 24 commits into from
May 21, 2020

Conversation

Satyam52
Copy link
Contributor

Fixes #7008

Short description of what this resolves:

removed duplicates in exception.py and the corresponding files which were importing exception from app/api/helpers/exception.py.

Checklist

  • I have read the Contribution & Best practices Guide and my PR follows them.
  • My branch is up-to-date with the Upstream development branch.
  • The unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • All the functions created/modified in this PR contain relevant docstrings.

app/api/tax.py Outdated
ForbiddenException,
MethodNotAllowed,
ConflictException,
MethodNotAllowed,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continuation line unaligned for hanging indent

app/api/tax.py Outdated
ConflictException,
ForbiddenException,
MethodNotAllowed,
ConflictException,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.
trailing whitespace

# "ticket sales-starts-at should be before event ends-at")

# if 'event_ends_at' in data and data['sales_ends_at'] > data['event_ends_at']:
# raise UnprocessableEntity({'pointer': '/data/attributes/sales-ends-at'},
# raise UnprocessableEntityError({'pointer': '/data/attributes/sales-ends-at'},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (91 > 90 characters)

{'pointer': '/data/attributes/sales-ends-at'},
"sales-ends-at should be after sales-starts-at",
)

# if 'event_ends_at' in data and data['sales_starts_at'] > data['event_ends_at']:
# raise UnprocessableEntity({'pointer': '/data/attributes/sales-starts-at'},
# raise UnprocessableEntityError({'pointer': '/data/attributes/sales-starts-at'},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (93 > 90 characters)

@@ -5,8 +5,7 @@
from marshmallow_jsonapi import fields
from marshmallow_jsonapi.flask import Relationship
from sqlalchemy.orm.exc import NoResultFound

from app.api.helpers.exceptions import ForbiddenException, UnprocessableEntity
from app.api.helpers.errors import ForbiddenError,UnprocessableEntityError

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.
missing whitespace after ','

@@ -5,7 +5,8 @@
from app.api.bootstrap import api
from app.api.helpers.db import save_to_db
from app.api.helpers.errors import NotFoundError
from app.api.helpers.exceptions import ForbiddenException, UnprocessableEntity
from app.api.helpers.errors import ForbiddenError,UnprocessableEntityError

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.
missing whitespace after ','

@@ -636,7 +637,7 @@ def before_get(self, args, kwargs):
if not has_access(
'is_coorganizer', event_id=order.event_id, user_id=order.user_id
):
raise ForbiddenException(
raiseForbiddenError(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'raiseForbiddenError'

@@ -586,7 +587,7 @@ def before_delete_object(self, order, view_kwargs):
:return:
"""
if not has_access('is_coorganizer', event_id=order.event.id):
raise ForbiddenException({'source': ''}, 'Access Forbidden')
raiseForbiddenError({'source': ''}, 'Access Forbidden')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'raiseForbiddenError'

@@ -470,7 +471,7 @@ def before_update_object(self, order, data, view_kwargs):
and data[element] != getattr(order, element, None)
and element not in get_updatable_fields()
):
raise ForbiddenException(
raiseForbiddenError(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'raiseForbiddenError'

@@ -459,7 +460,7 @@ def before_update_object(self, order, data, view_kwargs):
and order.status == 'completed'
and data[element] != getattr(order, element, None)
):
raise ForbiddenException(
raiseForbiddenError(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'raiseForbiddenError'

@@ -447,7 +448,7 @@ def before_update_object(self, order, data, view_kwargs):

elif current_user.id == order.user_id:
if order.status != 'initializing' and order.status != 'pending':
raise ForbiddenException(
raiseForbiddenError(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'raiseForbiddenError'

{'pointer': 'data/status'},
"You cannot update the status of a completed paid order",
)
elif element == 'status' and order.status == 'cancelled':
# Since the tickets have been unlocked and we can't revert it.
raise ForbiddenException(
raiseForbiddenError(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'raiseForbiddenError'

@@ -432,13 +433,13 @@ def before_update_object(self, order, data, view_kwargs):
and order.status == 'completed'
):
# Since we don't have a refund system.
raise ForbiddenException(
raiseForbiddenError(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'raiseForbiddenError'

@@ -422,7 +423,7 @@ def before_update_object(self, order, data, view_kwargs):
element
] != getattr(order, element, None):
if element != 'status' and element != 'deleted_at':
raise ForbiddenException(
raiseForbiddenError(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'raiseForbiddenError'

@@ -407,7 +408,7 @@ def before_update_object(self, order, data, view_kwargs):
and data[element] != getattr(order, element, None)
and element not in get_updatable_fields()
):
raise ForbiddenException(
raiseForbiddenError(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'raiseForbiddenError'

@@ -63,7 +64,7 @@ def check_event_user_ticket_holders(order, data, element):
if element in ['event', 'user'] and data[element] != str(
getattr(order, element, None).id
):
raise ForbiddenException(
raiseForbiddenError(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'raiseForbiddenError'

ConflictException
)
from app.api.helpers.errors import (
ForbiddenError,UnprocessableEntityError

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing whitespace after ','

UnprocessableEntity,
ConflictException
)
from app.api.helpers.errors import (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'app.api.helpers.errors.ForbiddenError' imported but unused

from app.api.helpers.exceptions import UnprocessableEntity
from app.api.helpers.errors import (
UnprocessableEntityError

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line contains whitespace

@@ -9,7 +9,10 @@
from flask import current_app
from itsdangerous import Serializer

from app.api.helpers.exceptions import UnprocessableEntity
from app.api.helpers.errors import (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.

@Satyam52 Satyam52 changed the title Duplicates Removed duplicate exceptions in api/helpers/exception.py which are already present in api/helpers/error.py May 16, 2020
@lgtm-com
Copy link
Contributor

lgtm-com bot commented May 16, 2020

This pull request introduces 2 alerts when merging eb1a214 into 069d8f8 - view on LGTM.com

new alerts:

  • 2 for Unused import

@@ -6,9 +6,12 @@
from app.api.helpers.db import safe_query
from app.api.helpers.exceptions import (
ConflictException,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the love with ConflictException? Why leave it?

@iamareebjamal
Copy link
Member

Syntactically wrong as well and the tests are also failing

@@ -4,7 +4,7 @@

from app.api.bootstrap import api
from app.api.helpers.db import safe_query
from app.api.helpers.exceptions import ForbiddenException, UnprocessableEntity
from app.api.helpers.errors import ForbiddenError,UnprocessableEntityError

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.
missing whitespace after ','

@@ -4,7 +4,7 @@

from app.api.bootstrap import api
from app.api.helpers.db import safe_query
from app.api.helpers.exceptions import UnprocessableEntity
from app.api.helpers.errors import UnprocessableEntityError

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'app.api.helpers.errors.UnprocessableEntityError' imported but unused

@@ -3,7 +3,7 @@

from app.api.bootstrap import api
from app.api.helpers.db import safe_query
from app.api.helpers.exceptions import ForbiddenException, UnprocessableEntity
from app.api.helpers.errors import ForbiddenError,UnprocessableEntityError

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.
missing whitespace after ','

@@ -498,7 +500,7 @@ def before_get(self, args, kwargs):
elif discount.used_for == 'event' and has_access('is_admin'):
self.schema = DiscountCodeSchemaEvent
else:
raise UnprocessableEntity({'source': ''}, "Please verify your permission")
raise UnprocessableEntityError({'source': ''}, "Please verify your permission")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (91 > 90 characters)

@@ -465,7 +467,7 @@ def before_get(self, args, kwargs):
elif discount.used_for == 'event' and has_access('is_admin'):
self.schema = DiscountCodeSchemaEvent
else:
raise UnprocessableEntity({'source': ''}, "Please verify your permission")
raise UnprocessableEntityError({'source': ''}, "Please verify your permission")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (91 > 90 characters)

@@ -372,10 +374,10 @@ def before_get_object(self, view_kwargs):
elif discount.used_for == 'event':
self.schema = DiscountCodeSchemaEvent
else:
raise UnprocessableEntity({'source': ''}, "Please verify your permission")
raise UnprocessableEntityError({'source': ''}, "Please verify your permission")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (95 > 90 characters)

@@ -308,7 +310,7 @@ def before_get(self, args, kwargs):
elif discount.used_for == 'event':
self.schema = DiscountCodeSchemaEvent
else:
raise UnprocessableEntity({'source': ''}, "Please verify your permission")
raise UnprocessableEntityError({'source': ''}, "Please verify your permission")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (95 > 90 characters)

MethodNotAllowed,
UnprocessableEntity,
)
from app.api.helpers.errors import (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.

@@ -160,7 +163,7 @@ def before_get(self, args, kwargs):
raise ObjectNotFound({'parameter': '{id}'}, "Access Code: not found")

if not has_access('is_coorganizer', event_id=access.event_id):
raise UnprocessableEntity({'source': ''}, "Please verify your permission")
raise UnprocessableEntityError({'source': ''}, "Please verify your permission")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line too long (95 > 90 characters)

)
from app.api.helpers.errors import (
UnprocessableEntityError ,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whitespace before ','

@@ -25,7 +25,7 @@ def test_reject_tickets_of_different_events(db):
db.session.commit()

with pytest.raises(
UnprocessableEntity, match=r'All tickets must belong to same event. Found: .*'
UnprocessableEntityError, match=r'All tickets must belong to same event. Found: .*'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.
line too long (91 > 90 characters)

@codecov
Copy link

codecov bot commented May 17, 2020

Codecov Report

Merging #7019 into development will decrease coverage by 0.02%.
The diff coverage is 25.73%.

Impacted file tree graph

@@               Coverage Diff               @@
##           development    #7019      +/-   ##
===============================================
- Coverage        60.55%   60.53%   -0.03%     
===============================================
  Files              260      259       -1     
  Lines            12877    12868       -9     
===============================================
- Hits              7798     7789       -9     
  Misses            5079     5079              
Impacted Files Coverage Δ
app/api/custom/orders.py 19.17% <ø> (ø)
app/api/custom_placeholders.py 53.06% <ø> (ø)
app/api/custom_system_roles.py 67.74% <0.00%> (ø)
app/api/data_layers/EventCopyLayer.py 20.22% <ø> (ø)
app/api/event_statistics.py 80.00% <0.00%> (ø)
app/api/faqs.py 79.54% <0.00%> (ø)
app/api/helpers/db.py 98.18% <0.00%> (ø)
app/api/helpers/permission_manager.py 28.77% <ø> (ø)
app/api/helpers/query.py 43.47% <0.00%> (ø)
app/api/notifications.py 76.27% <ø> (ø)
... and 47 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a914592...3f8b202. Read the comment docs.

ConflictException,
ForbiddenException,
UnprocessableEntity,
ConflictException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this left?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't find conflictException in other file(error.py).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then move it there

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so I should remove all exceptions from exception.py and move to error.py.there are only two left ConflictException and MethodNotAllowed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iamareebjamal Shall I move all exception from exception.py to error.py ???

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup

ForbiddenException,
MethodNotAllowed,
UnprocessableEntity,
MethodNotAllowed

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.

from app.api.helpers.errors import (
ForbiddenError,
UnprocessableEntityError,
ConflictException

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.

app/api/users.py Outdated
from app.api.helpers.errors import (
ForbiddenError,
UnprocessableEntityError,
ConflictException

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.

app/api/tax.py Outdated
from app.api.bootstrap import api
from app.api.helpers.db import get_count, safe_query
from app.api.helpers.exceptions import (
from app.api.helpers.errors import (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.

from app.api.helpers.errors import (
ForbiddenError,
UnprocessableEntityError,
ConflictException

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.

@@ -4,8 +4,7 @@

from app.api.bootstrap import api
from app.api.helpers.db import save_to_db
from app.api.helpers.errors import NotFoundError
from app.api.helpers.exceptions import ForbiddenException, UnprocessableEntity
from app.api.helpers.errors import ForbiddenError, UnprocessableEntityError, NotFoundError

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.

from app.api.helpers.errors import (
ForbiddenError,
UnprocessableEntityError,
ConflictException

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.

ForbiddenException,
MethodNotAllowed,
UnprocessableEntity,
MethodNotAllowed

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.

from app.api.helpers.errors import (
ForbiddenError,
UnprocessableEntityError,
ConflictException

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.

from app.api.helpers.errors import (
UnprocessableEntityError,
ForbiddenError,
ConflictException

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.

@iamareebjamal
Copy link
Member

Remove exceptions.py and resolve conflicts

@iamareebjamal
Copy link
Member

There are still merge conflicts

@Satyam52
Copy link
Contributor Author

@iamareebjamal resolved merge conflicts.

@iamareebjamal
Copy link
Member

Great work but I don't think you have pre-commit set up correctly. I understand, unfortunately, there is no automated way to enable it automatically. So you should run pre-commit install so that it automatically reformats files when you commit things.

Anyway I have created a PR to reformat files after your safe_query refactor #7020, so once it is merged, please rebase your branch and resolve conflicts and run black . so that it automatically formats the files for you according to the standard. Then commit and push the changes.
Thanks

@Satyam52
Copy link
Contributor Author

Satyam52 commented May 18, 2020

So, I should install pre-commit and then rebase with development branch and run black . before commiting and pushing to my branch ?

@iamareebjamal
Copy link
Member

Yup

@@ -25,7 +25,8 @@ def test_reject_tickets_of_different_events(db):
db.session.commit()

with pytest.raises(
UnprocessableEntity, match=r'All tickets must belong to same event. Found: .*'
UnprocessableEntityError,
match=r'All tickets must belong to same event. Found: .*',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.

from app.api.helpers.db import safe_query_kwargs
from app.api.helpers.exceptions import ForbiddenException
from app.api.helpers.errors import ForbiddenError

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redefinition of unused 'ForbiddenError' from line 4

from app.api.helpers.db import safe_query_kwargs
from app.api.helpers.exceptions import UnprocessableEntity
from app.api.helpers.errors import UnprocessableEntityError

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redefinition of unused 'UnprocessableEntityError' from line 4

)
from app.api.helpers.errors import BadRequestError
from app.api.helpers.exceptions import (
from app.api.helpers.db import safe_query, save_to_db, safe_query_kwargs, safe_query_by_id

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.

from app.api.helpers.db import safe_query_kwargs
from app.api.helpers.exceptions import ForbiddenException
from app.api.helpers.errors import ForbiddenError

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redefinition of unused 'ForbiddenError' from line 4

from app.api.helpers.db import safe_query, safe_query_kwargs
from app.api.helpers.exceptions import ForbiddenException, UnprocessableEntity
from app.api.helpers.errors import ForbiddenError, UnprocessableEntityError

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redefinition of unused 'ForbiddenError' from line 6
redefinition of unused 'UnprocessableEntityError' from line 6

from app.api.helpers.db import safe_query_kwargs
from app.api.helpers.exceptions import ForbiddenException
from app.api.helpers.errors import ForbiddenError

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redefinition of unused 'ForbiddenError' from line 4

@@ -70,29 +70,25 @@

def validate_event(user, modules, data):
if not user.can_create_event():
raise ForbiddenException({'source': ''}, "Please verify your Email")
raise ForbiddenError({'source': ''}, "Please verify your Email")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.

@Satyam52
Copy link
Contributor Author

Screenshot from 2020-05-20 19-23-07

UnprocessableEntityError,
ConflictException,
)
from app.api.helpers.db import safe_query, safe_query_kwargs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change? Revert

@@ -1,13 +1,13 @@
from flask_rest_jsonapi import ResourceDetail, ResourceList, ResourceRelationship
from flask_rest_jsonapi.exceptions import ObjectNotFound
from sqlalchemy.orm.exc import NoResultFound

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove this line?

from app.api.bootstrap import api

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add a line here?

ConflictException,
ForbiddenException,
UnprocessableEntity,
from app.api.helpers.db import safe_query, safe_query_by_id, safe_query_kwargs, save_to_db

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.

@iamareebjamal iamareebjamal changed the title Removed duplicate exceptions in api/helpers/exception.py which are already present in api/helpers/error.py chore(refactor): Move all custom API errors to errors.py May 21, 2020
@iamareebjamal iamareebjamal merged commit afc987a into fossasia:development May 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove duplicate exceptions in api/helpers/errors and exceptions.py
3 participants