Skip to content

Commit

Permalink
raise SxapiAuthorizationError on 403, add status_code to error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Mopsgeschwindigkeit committed Feb 2, 2024
1 parent 597689a commit c62fd6c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/sxapi/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def check_response(func):

def wrapper(*args, **kwargs):
response = func(*args, **kwargs)
if response.status_code == 401:
raise SxapiAuthorizationError()
if response.status_code in [401, 403]:
raise SxapiAuthorizationError(status_code=response.status_code)
elif response.status_code == 422:
raise SxapiUnprocessableContentError()
else:
Expand Down
24 changes: 18 additions & 6 deletions src/sxapi/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,38 @@ class SxapiAuthorizationError(Exception):

AUTHORIZATION_ERROR_MSG = """
Requested object could not be accessed.
Maybe the organisation_id or the _id of the requested object is wrong."""
Maybe the organisation_id or the _id of the requested object is wrong.
"""

def __init__(self, message=AUTHORIZATION_ERROR_MSG):
def __init__(self, message=AUTHORIZATION_ERROR_MSG, status_code=None):
self.message = message
self.status_code = status_code

def __str__(self):
return self.message
text = self.message
if self.status_code:
text = text + "status code: " + str(self.status_code)

return text


class SxapiUnprocessableContentError(Exception):
"""Raised when content is unprocessable 422."""

UNPROCESSABLE_CONTENT_ERROR_MSG = """
The request was well-formed but was unable to be followed due to semantic errors."""
The request was well-formed but was unable to be followed due to semantic errors.
"""

def __init__(self, message=UNPROCESSABLE_CONTENT_ERROR_MSG):
def __init__(self, message=UNPROCESSABLE_CONTENT_ERROR_MSG, status_code=None):
self.message = message
self.status_code = status_code

def __str__(self):
return self.message
text = self.message
if self.status_code:
text = text + "status code: " + str(self.status_code)

return text


class SxapiConfigurationFileError(Exception):
Expand Down

0 comments on commit c62fd6c

Please sign in to comment.