Skip to content

Commit 6534573

Browse files
author
Laurynas Butkus
committed
Add error handling
1 parent 45c2deb commit 6534573

File tree

5 files changed

+31
-9
lines changed

5 files changed

+31
-9
lines changed

convertapi/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from .exceptions import *
44
from .client import Client
55
from .upload_io import UploadIO
6-
from .api import convert
6+
from .api import convert, user
77

88
# configuration
99

convertapi/api.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
import convertapi
2+
13
from .task import Task
24

35
def convert(to_format, params, from_format = None, timeout = None):
46
task = Task(from_format, to_format, params, timeout = timeout)
57
return task.run()
8+
9+
def user(timeout = None):
10+
return convertapi.client.get('user', timeout = timeout)

convertapi/client.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ def download(self, url, path):
3737
return path
3838

3939
def handle_response(self, r):
40-
r.raise_for_status()
41-
42-
# if r.status_code >= 400:
43-
# raise ConvertApiError(json['Message'])
40+
try:
41+
r.raise_for_status()
42+
except requests.RequestException as e:
43+
try:
44+
raise ApiError(r.json())
45+
except ValueError:
46+
raise e
4447

4548
return r.json()
4649

convertapi/exceptions.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1-
class ConvertApiError(BaseException):
2-
def __init__(self, *args, **kwargs):
3-
super(ConvertApiError, self).__init__(*args, **kwargs)
1+
class BaseError(BaseException):
2+
pass
3+
4+
class ApiError(BaseError):
5+
def __init__(self, result):
6+
super(ApiError, self).__init__(result['Message'])
7+
8+
self.code = result['Code']
9+
self.invalid_parameters = result['InvalidParameters']
10+
11+
def __str__(self):
12+
return "%s Code: %s. %s" % (self.message, self.code, self.invalid_parameters)

tests/test_convertapi.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
import os
33
import io
44
import tempfile
5+
import requests
56

67
from . import utils
7-
from nose.tools import eq_
8+
from nose.tools import *
89

910
class TestConvertapi(utils.TestCase):
1011
def setUp(self):
@@ -45,3 +46,7 @@ def test_chained_conversion(self):
4546
result = convertapi.convert('pdf', { 'File': 'examples/files/test.docx' })
4647
zip_result = convertapi.convert('zip', { 'Files': result.files })
4748
eq_('test.zip', zip_result.file.filename)
49+
50+
@raises(convertapi.ApiError)
51+
def test_api_error(self):
52+
convertapi.convert('pdf', { 'Url': 'https://www.w3.org/TR/PNG/iso_8859-1.txt' }, 'web', 600)

0 commit comments

Comments
 (0)