Skip to content

Commit

Permalink
Add specific error handling for the HTTP 403 error case, which indica…
Browse files Browse the repository at this point in the history
…te the user is authorized but needs a paid plan.
  • Loading branch information
ChrisLoer committed Jan 14, 2025
1 parent 623e2e1 commit 6b2006b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 20 deletions.
8 changes: 8 additions & 0 deletions felt/core/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class CreatedGroupDetails:
ordering_key: Optional[int] = None


class PaidPlanRequiredError(Exception):
"""Raised when an API operation requires a paid plan"""
pass


class FeltApiClient:
"""
Client for the Felt API
Expand Down Expand Up @@ -512,6 +517,9 @@ def create_layer_groups(self,
json.dumps(group_post_data).encode()
)

if reply.error() == QNetworkReply.ContentAccessDenied:
raise PaidPlanRequiredError("Upload requires a paid plan")

return [
CreatedGroupDetails(
group_id=group['id'],
Expand Down
42 changes: 31 additions & 11 deletions felt/core/map_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
)
from qgis.utils import iface

from .api_client import API_CLIENT
from .api_client import API_CLIENT, PaidPlanRequiredError
from .enums import LayerSupport
from .exceptions import LayerPackagingException
from .layer_exporter import LayerExporter
Expand Down Expand Up @@ -279,6 +279,7 @@ def __init__(self,
self.error_string: Optional[str] = None
self.feedback: Optional[QgsFeedback] = None
self.was_canceled = False
self.paid_plan_error = False

@staticmethod
def layer_and_group(
Expand Down Expand Up @@ -507,6 +508,8 @@ def run(self):
)

if reply.error() != QNetworkReply.NoError:
if reply.error() == QNetworkReply.ContentAccessDenied:
self.paid_plan_error = True
self.error_string = reply.errorString()
Logger.instance().log_error_json(
{
Expand Down Expand Up @@ -604,15 +607,27 @@ def run(self):
all_group_ordering_keys = self.project_structure.group_ordering_keys()
if all_group_ordering_keys:
# ensure group names match their order in the QGIS project
created_groups = API_CLIENT.create_layer_groups(
map_id=self.associated_map.id,
layer_group_names=list(all_group_ordering_keys.keys()),
ordering_keys=all_group_ordering_keys
)
created_group_details = {
group.name: group
for group in created_groups
}
try:
created_groups = API_CLIENT.create_layer_groups(
map_id=self.associated_map.id,
layer_group_names=list(all_group_ordering_keys.keys()),
ordering_keys=all_group_ordering_keys
)
created_group_details = {
group.name: group
for group in created_groups
}
except PaidPlanRequiredError:
self.paid_plan_error = True
self.error_string = 'Paid plan required for layer groups'
Logger.instance().log_error_json(
{
'type': Logger.MAP_EXPORT,
'error': 'Error creating layer groups: {}'.format(
self.error_string)
}
)
return False
else:
created_group_details = {}

Expand Down Expand Up @@ -655,6 +670,8 @@ def run(self):
continue

if reply.error() != QNetworkReply.NoError:
if reply.error() == QNetworkReply.ContentAccessDenied:
self.paid_plan_error = True
self.error_string = reply.errorString()
Logger.instance().log_error_json(
{
Expand All @@ -663,7 +680,6 @@ def run(self):
self.error_string)
}
)

return False
break

Expand Down Expand Up @@ -769,6 +785,8 @@ def _upload_progress(sent, total):
)

if reply and reply.error() != QNetworkReply.NoError:
if reply.error() == QNetworkReply.ContentAccessDenied:
self.paid_plan_error = True
self.error_string = reply.errorString()
Logger.instance().log_error_json(
{
Expand Down Expand Up @@ -806,6 +824,8 @@ def _upload_progress(sent, total):
)

if reply and reply.error() != QNetworkReply.NoError:
if reply.error() == QNetworkReply.ContentAccessDenied:
self.paid_plan_error = True
self.error_string = reply.errorString()
Logger.instance().log_error_json(
{
Expand Down
25 changes: 16 additions & 9 deletions felt/gui/create_map_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,16 +538,23 @@ def _upload_terminated(self):
self._map_title)
)

error_message = \
self.tr('There was an error uploading this file, please '
'contact <a href="mailto:[email protected]">'
'[email protected]</a> '
'for help fixing the issue')

if self.map_uploader_task.error_string:
error_message += '<p><b>{}</b></p>'.format(
self.map_uploader_task.error_string
if self.map_uploader_task.paid_plan_error:
error_message = self.tr(
"Uploading files to Felt requires a paid plan, please visit "
"<a href='https://felt.com/pricing'>felt.com/pricing</a> or contact "
"<a href='mailto:[email protected]'>[email protected]</a>."
)
else:
error_message = \
self.tr('There was an error uploading this file, please '
'contact <a href="mailto:[email protected]">'
'[email protected]</a> '
'for help fixing the issue')

if self.map_uploader_task.error_string:
error_message += '<p><b>{}</b></p>'.format(
self.map_uploader_task.error_string
)

self.progress_label.setText(
GuiUtils.set_link_color(
Expand Down

0 comments on commit 6b2006b

Please sign in to comment.