Skip to content

Commit 80ff58d

Browse files
Fix for list which raises an error (#247)
* Fix for list which raises an error * Test for raising error * PEP fix
1 parent 42aa14c commit 80ff58d

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

inventree/base.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
import json
44
import logging
5+
import requests
56
import os
67

78
from . import api as inventree_api
89

9-
INVENTREE_PYTHON_VERSION = "0.17.1"
10+
INVENTREE_PYTHON_VERSION = "0.17.2"
1011

1112

1213
logger = logging.getLogger('inventree')
@@ -231,10 +232,21 @@ def list(cls, api, **kwargs):
231232
else:
232233
url = cls.URL
233234

234-
response = api.get(url=url, params=kwargs)
235+
try:
236+
response = api.get(url=url, params=kwargs)
237+
except requests.exceptions.HTTPError as e:
238+
logger.error(f"Error during list request: {e}")
239+
# Return an empty list
240+
241+
raise_error = kwargs.get('raise_error', False)
242+
243+
if raise_error:
244+
raise e
245+
else:
246+
return []
235247

236248
if response is None:
237-
return None
249+
return []
238250

239251
items = []
240252

test/test_order.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,43 @@ def test_po_attachment(self):
390390
attachments = po.getAttachments()
391391
self.assertEqual(len(attachments), 3)
392392

393+
def test_invalid_list(self):
394+
"""Test list with an invalid parameter.
395+
396+
Ref: https://github.com/inventree/inventree-python/issues/246
397+
"""
398+
399+
from inventree.project_code import ProjectCode
400+
401+
results = order.PurchaseOrder.list(self.api, project_code=999999999)
402+
self.assertEqual(len(results), 0)
403+
404+
# Try the same again, but raise the eror
405+
with self.assertRaises(HTTPError):
406+
results = order.PurchaseOrder.list(
407+
self.api,
408+
project_code=999999999,
409+
raise_error=True
410+
)
411+
412+
# Find a valid project code
413+
n = ProjectCode.count(self.api)
414+
415+
# Create a new project code
416+
pc = ProjectCode.create(self.api, {
417+
'code': f"TEST-{n+1}",
418+
'description': 'Test project code',
419+
})
420+
421+
# Attach project code to an order
422+
po = order.PurchaseOrder.list(self.api, limit=1)[0]
423+
po.save({'project_code': pc.pk})
424+
425+
# Now, list orders with the project code
426+
results = order.PurchaseOrder.list(self.api, project_code=pc.pk)
427+
self.assertEqual(len(results), 1)
428+
self.assertEqual(results[0].pk, po.pk)
429+
393430

394431
class SOTest(InvenTreeTestCase):
395432
"""

0 commit comments

Comments
 (0)