Skip to content

Commit

Permalink
Added some exception handling for XML data that has too large of an I…
Browse files Browse the repository at this point in the history
…NT in it. #2201
  • Loading branch information
allmightyspiff committed Dec 17, 2024
1 parent deaa4eb commit e7de42b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 19 deletions.
58 changes: 42 additions & 16 deletions SoftLayer/fixtures/SoftLayer_Billing_Invoice.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,49 @@
getInvoiceTopLevelItems = [
{
'categoryCode': 'sov_sec_ip_addresses_priv',
'createDate': '2018-04-04T23:15:20-06:00',
'description': '64 Portable Private IP Addresses',
'id': 724951323,
'oneTimeAfterTaxAmount': '0',
'recurringAfterTaxAmount': '0',
'hostName': 'bleg',
'domainName': 'beh.com',
'category': {'name': 'Private (only) Secondary VLAN IP Addresses'},
'children': [
"categoryCode": "sov_sec_ip_addresses_priv",
"createDate": "2018-04-04T23:15:20-06:00",
"description": "64 Portable Private IP Addresses",
"id": 724951323,
"oneTimeAfterTaxAmount": "0",
"recurringAfterTaxAmount": "0",
"hostName": "bleg",
"domainName": "beh.com",
"category": {"name": "Private (only) Secondary VLAN IP Addresses"},
"children": [
{
'id': 12345,
'category': {'name': 'Fake Child Category'},
'description': 'Blah',
'oneTimeAfterTaxAmount': 55.50,
'recurringAfterTaxAmount': 0.10
"id": 12345,
"category": {"name": "Fake Child Category"},
"description": "Blah",
"oneTimeAfterTaxAmount": 55.50,
"recurringAfterTaxAmount": 0.10
}
],
'location': {'name': 'fra02'}
"location": {"name": "fra02"}
},
{
"categoryCode": "reserved_capacity",
"createDate": "2024-07-03T22:08:36-07:00",
"description": "B1.1x2 (1 Year Term) (721hrs * .025)",
"id": 1111222,
"oneTimeAfterTaxAmount": "0",
"recurringAfterTaxAmount": "18.03",
"category": {"name": "Reserved Capacity"},
"children": [
{
"description": "1 x 2.0 GHz or higher Core",
"id": 29819,
"oneTimeAfterTaxAmount": "0",
"recurringAfterTaxAmount": "10.00",
"category": {"name": "Computing Instance"}
},
{
"description": "2 GB",
"id": 123456,
"oneTimeAfterTaxAmount": "0",
"recurringAfterTaxAmount": "2.33",
"category": {"name": "RAM"}
}
],
"location": {"name": "dal10"}
}
]
26 changes: 24 additions & 2 deletions SoftLayer/testing/xmlrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@
~~~~~~~~~~~~~~~~~~~~~~~~
XMP-RPC server which can use a transport to proxy requests for testing.
If you want to spin up a test XML server to make fake API calls with, try this:
quick-server.py
---
import SoftLayer
from SoftLayer.testing import xmlrpc
my_xport = SoftLayer.FixtureTransport()
my_server = xmlrpc.create_test_server(my_xport, "localhost", port=4321)
print(f"Server running on http://{my_server.server_name}:{my_server.server_port}")
---
$> python quick-server.py
$> curl -X POST -d "<?xml version='1.0' encoding='iso-8859-1'?><methodCall><methodName>getInvoiceTopLevelItems</methodName><params><param><value><struct><member><name>headers</name><value><struct><member><name>SoftLayer_Billing_InvoiceInitParameters</name><value><struct><member><name>id</name><value><string>1234</string></value></member></struct></value></member></struct></value></member></struct></value></param></params></methodCall>" http://127.0.0.1:4321/SoftLayer_Billing_Invoice
:license: MIT, see LICENSE for more details.
"""
import http.server
Expand Down Expand Up @@ -60,6 +74,7 @@ def do_POST(self):
self.send_response(200)
self.send_header("Content-type", "application/xml; charset=UTF-8")
self.end_headers()

try:
self.wfile.write(response_body.encode('utf-8'))
except UnicodeDecodeError:
Expand All @@ -78,9 +93,16 @@ def do_POST(self):
response = xmlrpc.client.Fault(ex.faultCode, str(ex.reason))
response_body = xmlrpc.client.dumps(response, allow_none=True, methodresponse=True)
self.wfile.write(response_body.encode('utf-8'))
except Exception:
except OverflowError as ex:
self.send_response(555)
self.send_header("Content-type", "application/xml; charset=UTF-8")
self.end_headers()
response_body = '''<error>OverflowError in XML response.</error>'''
self.wfile.write(response_body.encode('utf-8'))
logging.exception(f"Error while handling request: {str(ex)}")
except Exception as ex:
self.send_response(500)
logging.exception("Error while handling request")
logging.exception(f"Error while handling request: {str(ex)}")

def log_message(self, fmt, *args):
"""Override log_message."""
Expand Down
3 changes: 2 additions & 1 deletion SoftLayer/transports/xmlrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ def __call__(self, request):
_ex = error_mapping.get(ex.faultCode, exceptions.SoftLayerAPIError)
raise _ex(ex.faultCode, ex.faultString) from ex
except requests.HTTPError as ex:
raise exceptions.TransportError(ex.response.status_code, str(ex))
err_message = f"{str(ex)} :: {ex.response.content}"
raise exceptions.TransportError(ex.response.status_code, err_message)
except requests.RequestException as ex:
raise exceptions.TransportError(0, str(ex))

Expand Down

0 comments on commit e7de42b

Please sign in to comment.