Skip to content

Commit

Permalink
Merge pull request #1015 from opengisch/QF-2134_fix-sentry-logging
Browse files Browse the repository at this point in the history
Make the information whether an error should be logged to sentry  part of the error class
  • Loading branch information
suricactus authored Sep 9, 2024
2 parents 92f5246 + c069aa9 commit d94510b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
19 changes: 17 additions & 2 deletions docker-app/qfieldcloud/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,21 @@ class IntegrationError(QfcError): ...


class QFieldCloudException(Exception):
"""Generic QFieldCloud Exception"""
"""Generic QFieldCloud Exception
Attributes:
code (str): error code
message (str): error message.
status_code (int): HTTP status code to be returned by the global Django error handler.
log_as_error (bool): If set to `False`, the error will be logged as info level, instead of error level.
This is useful for error that do not show problems in the system, but are server side client data assertions.
Default is True.
"""

code = "unknown_error"
message = "QFieldcloud Unknown Error"
status_code = None
log_as_error = True

def __init__(self, detail="", status_code=None):
self.detail = detail
Expand Down Expand Up @@ -44,6 +54,7 @@ class AuthenticationFailedError(QFieldCloudException):
code = "authentication_failed"
message = "Authentication failed"
status_code = status.HTTP_401_UNAUTHORIZED
log_as_error = False


class AuthenticationViaTokenFailedError(QFieldCloudException):
Expand All @@ -60,6 +71,7 @@ class NotAuthenticatedError(QFieldCloudException):
code = "not_authenticated"
message = "Not authenticated"
status_code = status.HTTP_401_UNAUTHORIZED
log_as_error = False


class TooManyLoginAttemptsError(QFieldCloudException):
Expand All @@ -76,6 +88,7 @@ class PermissionDeniedError(QFieldCloudException):
code = "permission_denied"
message = "Permission denied"
status_code = status.HTTP_403_FORBIDDEN
log_as_error = False


class EmptyContentError(QFieldCloudException):
Expand All @@ -84,7 +97,7 @@ class EmptyContentError(QFieldCloudException):

code = "empty_content"
message = "Empty content"
status_code = status.HTTP_503_SERVICE_UNAVAILABLE
status_code = status.HTTP_400_BAD_REQUEST


class MultipleContentsError(QFieldCloudException):
Expand All @@ -103,6 +116,7 @@ class ObjectNotFoundError(QFieldCloudException):
code = "object_not_found"
message = "Object not found"
status_code = status.HTTP_400_BAD_REQUEST
log_as_error = False


class APIError(QFieldCloudException):
Expand All @@ -120,6 +134,7 @@ class ValidationError(QFieldCloudException):
code = "validation_error"
message = "Validation error"
status_code = status.HTTP_400_BAD_REQUEST
log_as_error = False


class MultipleProjectsError(QFieldCloudException):
Expand Down
7 changes: 2 additions & 5 deletions docker-app/qfieldcloud/core/rest_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

def exception_handler(exc, context):
# Map exceptions to qfc exceptions
is_error = False
if isinstance(exc, rest_exceptions.AuthenticationFailed):
qfc_exc = qfieldcloud_exceptions.AuthenticationFailedError()
elif isinstance(exc, rest_exceptions.NotAuthenticated):
Expand All @@ -23,19 +22,17 @@ def exception_handler(exc, context):
elif isinstance(exc, exceptions.ValidationError):
qfc_exc = qfieldcloud_exceptions.ValidationError(detail=str(exc))
elif isinstance(exc, qfieldcloud_exceptions.QFieldCloudException):
is_error = True
qfc_exc = exc
elif isinstance(exc, rest_exceptions.APIException):
is_error = True
qfc_exc = qfieldcloud_exceptions.APIError(exc.detail, exc.status_code)
else:
# Unexpected ! We rethrow original exception to make debugging tests easier
if settings.IN_TEST_SUITE:
raise exc
is_error = True
qfc_exc = qfieldcloud_exceptions.QFieldCloudException(detail=str(exc))

if is_error:
# Log level is defined by the exception
if qfc_exc.log_as_error:
# log the original exception
logging.exception(exc)
else:
Expand Down

0 comments on commit d94510b

Please sign in to comment.