Skip to content

Commit

Permalink
Merge pull request #99 from transifex/down_up_load_exceptions
Browse files Browse the repository at this point in the history
Refactor transifex.api's exceptions for down/uploads
  • Loading branch information
kbairak authored Feb 10, 2023
2 parents 5fd1f97 + a8a9b01 commit 3953fb3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 21 deletions.
32 changes: 11 additions & 21 deletions transifex/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import transifex

from .exceptions import DownloadException, UploadException
from .jsonapi import JsonApi
from .jsonapi import Resource as JsonApiResource
from .jsonapi.exceptions import JsonApiException


class DownloadMixin(object):
Expand All @@ -19,16 +19,11 @@ def download(cls, interval=5, *args, **kwargs):
download = cls.create(*args, **kwargs)
while True:
if hasattr(download, "errors") and len(download.errors) > 0:
errors = [
{
"code": e["code"],
"detail": e["detail"],
"title": e["detail"],
"status": "409",
}
for e in download.errors
]
raise JsonApiException(409, errors)
# The way Transifex APIv3 works right now, only one error will be
# returned, so we give priority to the first error's 'detail' field. If
# more errors are included in the future, the user can inspect the
# exception's second argument
raise DownloadException(download.error[0]["detail"], download.errors)
if download.redirect:
return download.redirect
time.sleep(interval)
Expand All @@ -54,16 +49,11 @@ def upload(cls, content, interval=5, **data):

while True:
if hasattr(upload, "errors") and len(upload.errors) > 0:
errors = [
{
"code": e["code"],
"detail": e["detail"],
"title": e["detail"],
"status": "409",
}
for e in upload.errors
]
raise JsonApiException(409, errors)
# The way Transifex APIv3 works right now, only one error will be
# returned, so we give priority to the first error's 'detail' field. If
# more errors are included in the future, the user can inspect the
# exception's second argument
raise UploadException(upload.errors[0]["detail"], upload.errors)

if upload.redirect:
return upload.follow()
Expand Down
35 changes: 35 additions & 0 deletions transifex/api/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class FileExchangeException(Exception):
def __init__(self, message, errors):
super().__init__(message, errors)

@property
def message(self):
return self.args[0]

@property
def errors(self):
return self.args[1]

def __str__(self):
"""We expect Transifex APIv3 to only return one error during a file exchange.
Even though for future compatibility we include the full list of errors in the
exception objects, we override 'str' so that if someone does:
>>> try:
... transifex_api.ResourceStringsAsyncUpload(...)
... except UploadException as e:
... print(f"Upload error: {e}")
We want the first error's 'detail' field to appear. The full error list
can still be accessed via the second argument.
"""

return self.message


class DownloadException(FileExchangeException):
pass


class UploadException(FileExchangeException):
pass

0 comments on commit 3953fb3

Please sign in to comment.